xaml - Databinding not working within resource dictionary -
i trying bind selecteditem property of listbox 2 textbox controls. these controls located in windows resource section. idea when entry in listbox gets selected 2 textbox controls display "blockname" , "blockhelptext" custom class. load listbox first clicking but1 button.
<window.resources> <stackpanel x:key="testsp" visibility="visible" datacontext="{binding elementname=lsbcommonblocks, path=selecteditem, diagnostics:presentationtracesources.tracelevel=high}"> <listbox x:name="lsbcommonblocks" displaymemberpath="blockname" selectionchanged="lb_sc"/> <button x:name="but1" click="but1_click" content="button 1"/> <textbox x:name="txt1" text="{binding path=blockname, diagnostics:presentationtracesources.tracelevel=high}"/> <textbox x:name="txt2" text="{binding path=blockhelptext, diagnostics:presentationtracesources.tracelevel=high}"/> </stackpanel> </window.resources> <grid> <contentcontrol visibility="visible" x:name="contentworkarea" content="{staticresource testsp}"/> </grid>
i have button who's click event ties itemsources property of listbox custom class blocktoolbar.
blocktoolbar[] blocks = { new blocktoolbar("block 1", "no block."), new blocktoolbar("block 2", "help."), new blocktoolbar("block 3", "help again.") }; private void but1_click(object sender, routedeventargs e) { stackpanel sp = (stackpanel)this.tryfindresource("testsp"); listbox lb = (listbox)logicaltreehelper.findlogicalnode(sp, "lsbcommonblocks"); lb.itemssource = blocks; } public class blocktoolbar : inotifypropertychanged { private string blockname; public string blockname { { return blockname; } set { blockname = value; onpropertychanged(new propertychangedeventargs("blockname")); } } private string blockhelptext; public string blockhelptext { { return blockhelptext; } set { blockhelptext = value; onpropertychanged(new propertychangedeventargs("blockhelptext")); } } public blocktoolbar() { blockname = ""; blockhelptext = ""; } public blocktoolbar(string blockname, string blockhelptext) { blockname = blockname; blockhelptext = blockhelptext; } public event propertychangedeventhandler propertychanged; public void onpropertychanged(propertychangedeventargs e) { if (propertychanged != null) propertychanged(this, e); } }
databinding not work: textbox's txt1 , txt2 remain blank when click button. listbox populates ok. confirmed i'm able obtain blocktoolbar properties ok creating event on listbox.
visual studio (express) output window gives me following: system.windows.data error: 4 : cannot find source binding reference 'elementname=lsbcommonblocks'. bindingexpression:path=selecteditem; dataitem=null; target element 'stackpanel' (name=''); target property 'datacontext' (type 'object')
not sure i'm doing wrong. i'm kind new wpf.
if remove stackpanel resource (testsp) , replace contentcontrol testsp controls, databindings work. can bind txt1 , txt2 directly blocktoolbar object in resource can't seem bind txt1/txt2 selecteditem if in resource.
the reason i'm doing have complicated main window break seperate resourcedictionaries , call main window via contentcontrols. when couldn't work create simpler project try narrow down problem.
looks have create binding in code. made following changes , worked. thought tried before but...
private void but1_click(object sender, routedeventargs e) { stackpanel sp = (stackpanel)this.tryfindresource("testsp"); listbox lb = (listbox)logicaltreehelper.findlogicalnode(sp, "lsbcommonblocks"); binding mybinding = new binding(); mybinding.source = lb; mybinding.path = new propertypath("selecteditem"); sp.setbinding(stackpanel.datacontextproperty, mybinding); lb.itemssource = blocks; }
Comments
Post a Comment