wpf - Binding mode for item in listbox -


when using datatemplate listbox item, app freezes , throws exception in output window. details follow.

edit template:

<datatemplate x:key="editonlytemplate">     <border borderbrush="blue" margin="3" padding="3" borderthickness="2" cornerradius="5" background="beige">         <stackpanel orientation="horizontal">             <stackpanel orientation="vertical" verticalalignment="center">                 <textbox width="300" fontsize="25" foreground="goldenrod" text="{binding xpath=/doc/dob/text, mode=onewaytosource, updatesourcetrigger=propertychanged, diag:presentationtracesources.tracelevel=high}" />                 <textbox width="300" fontsize="25" foreground="blue" text="{binding xpath=/doc/dob/group, updatesourcetrigger=propertychanged}" />                 <textbox width="300" fontsize="25" foreground="green" text="{binding xpath=/doc/dob/filter, updatesourcetrigger=propertychanged}" />                 <button content="save" click="btnsave_click" width="40" horizontalalignment="left" commandparameter="{binding relativesource={relativesource findancestor, ancestortype={x:type contentcontrol}}}"/>             </stackpanel>         </stackpanel>     </border> </datatemplate> 

i using following code behind handle button click , switch datatemplate on listbox item 1 displays bound data (ie, "details" template) 1 can modify bound data (ie, "edit" template).

private void btnedit_click(object sender, routedeventargs e) {   button btn = (button)sender;   //command contains list item   contentcontrol itm = (contentcontrol)btn.commandparameter;   itm.contenttemplate = this.findresource("edittemplate") datatemplate;  } 

the thing note edittemplate binding mode of first textbox - set onewaytosource. causes problem, upon clicking "edit" button swaps in template. if change mode "twoway" (the default mode textbox), there no issue. also, if use copy of template in place on page, placed in during design - not via code - , not in listbox item, rather, in contentcontrol onewaytosource mode works expected.

the exception thrown in broken scenario following:

system.windows.data warning: 104 : bindingexpression (hash=48690759):   @ level 0 - xmlelement.innertext found accessor reflectpropertydescriptor(innertext)     system.windows.data warning: 100 : bindingexpression (hash=48690759): replace item @ level 0 xmlelement (hash=21889970), using accessor reflectpropertydescriptor(innertext)     system.windows.data warning: 86 : bindingexpression (hash=48690759): update - got raw value ''     system.windows.data warning: 90 : bindingexpression (hash=48690759): update - using final value ''     system.wi  ndows.data warning: 98 : bindingexpression (hash=48690759): setvalue @ level 0 xmlelement (hash=21889970) using reflectpropertydescriptor(innertext): '' first chance exception of type 'system.invalidoperationexception' occurred in windowsbase.dll system.windows.data error: 8 : cannot save value target source. bindingexpression:path=/innertext; dataitem='xmldocument' (hashcode=33583636); target element 'textbox' (name=''); target property 'text' (type 'string') invalidoperationexception:'system.invalidoperationexception: dispatcher processing has been suspended, messages still being processed.    @ system.componentmodel.reflectpropertydescriptor.setvalue(object component, object value)    @ ms.internal.data.propertypathworker.setvalue(object item, object value)    @ ms.internal.data.clrbindingworker.updatevalue(object value)    @ system.windows.data.bindingexpression.updatesource(object value)' 

i suspect listbox somehow cause, cannot understand why, except possible bug. using .net framework 3.5 current updates, on windows xp. see wrong attempting do?

ignoring exception , helping past current implementation should making use of datatemplateselector. here sample takes place on listbox.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -