c# - WPF Binding data from two controls -


im trying bind custom property 1 control another, here code:

first, custom treeviewcontrol

public partial class itemtreecontrol : usercontrol {     readonly itemtreeviewmodel itemtreeviewmodel;      public static readonly dependencyproperty selecteditemproperty =          dependencyproperty.register("selecteditem",                                     typeof(item),                                      typeof(itemtreecontrol),                                     new propertymetadata(default(item)));      public item selecteditem     {         { return (item)getvalue(selecteditemproperty); }         set { setvalue(selecteditemproperty, value); }     } } 

here goes custom control displaying details:

public partial class itemdetailcontrol : usercontrol {     public static readonly dependencyproperty detailitemproperty =              dependencyproperty.register("detailitem",                                          typeof(item),                                          typeof(itemdetailcontrol),                                          new propertymetadata(default(item)));      public item detailitem     {         { return (item)getvalue(detailitemproperty); }         set { setvalue(detailitemproperty, value); }     } } 

now xaml code binding:

<stackpanel>     <itemtreecontrol x:name="itemtreecontrol"/>     <itemdetailcontrol detailitem="{binding elementname=itemtreecontrol,                                       path=selecteditem, mode=twoway}"/> </stackpanel> 

now, if new value arrives @ itemtreecontrol.selecteditem, value in itemdetailcontrol.detailitem dont change. why?

updated.

code-behind in itemtreecontrol:

private void mytreeview_selecteditemchanged(object sender, routedpropertychangedeventargs<object> e) {     if (e.newvalue itemviewmodel)     {          selecteditem = (e.newvalue itemviewmodel).item;     }     else     {         selecteditem = null;     } } 

in general ui controls not bind each other's properties. selecteditem ideal kept @ viewmodel level. if have vm underlying these controls, should bind both these controls vm-level property.

also note detail control doesn't change selecteditem, binding should not twoway.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -