c# - WPF User Control Dependency Property not working when bound to a ViewModel property -
i have 2 user-controls: locationtreeview, , locationpicker. locationtreeview organizes locations tree structure. because of number of locations involved, parts of tree loaded @ once (one level @ time items expanded).
the locationpicker little more textblock button opens modal window locationtreeview on it.
when bind locationpicker's "selectedlocation" property viewmodel, works fine. when bind locationtreeview viewmodel, binding doesn't seem have effect @ all. when bind locationtreeview "dummy" locationpicker (which bound viewmodel) works. how can locationtreeview bind viewmodel?
public partial class locationtreeview: usercontrol { public eventhandler locationchanged; ... public static readonly dependencyproperty selectedlocationproperty = dependencyproperty.register("selectedlocation",typeof(location), typeof(locationtreeview), new frameworkpropertymetadata(null, frameworkpropertymetadataoptions.bindstwowaybydefault, selectedlocationchanged)); ... public static void selectedlocationchanged(dependencyobject d, dependencypropertychangedeventargs e) { locationtreeview sender = (d locationtreeview); location loc = e.newvalue location; //navigate treeview selected location sender.loadltreeviewpathtolocation(loc); } public location selectedlocation { { return (location)getvalue(selectedlocationproperty); } set { if (selectedlocation != value) { setvalue(selectedlocationproperty, value); if (locationchanged != null) { locationchanged(this, eventargs.empty); } } } } ... } binding on control works fine when bound control, not when bound viewmodel. i've set breakpoint in selectedlocationchanged callback, doesn't seem fired when set viewmodel property (which implement inotifypropertychanged)
public partial class locationpicker: usercontrol { public static readonly dependencyproperty selectedlocationproperty = dependencyproperty.register("selectedlocation",typeof(location), typeof(locationpicker), new frameworkpropertymetadata(null, frameworkpropertymetadataoptions.bindstwowaybydefault)); ... public location selectedlocation { { return (location)getvalue(selectedlocationproperty); } set { setvalue(selectedlocationproperty, value); } } ... private void button_click(object sender, routedeventargs e) { // create window locationtreeview on it. set treeview's // selectedlocation property, open window, wait window close, // set this.selectedloctation treeview's selected location. } } i apologize leaving out code. work enviroment prevents me being able copy/paste.
i've left out code viewmodel. quite confident not issue.
update: locationtreeview has viewmodel set in xaml
<usercontrol.datacontext> <vm:locationtreeviewviewmodel /> </usercontrol.datacontext> the locationpicker not have viewmodel. on window using controls, xaml looks this
<widow.datacontext> <vm:testwindowviewmodel /> </window.datacontext> <grid> ... <uc:locationpicker x:name="picker" selectedlocation="{binding location}" /> <!-- not work --> <uc:locationtreeview selectedlocaiton="{binding location}" /> <!-- works ---> <uc:locationtreeview selectedlocaiton="{binding selectedlocation, elementname=picker}" /> ... </grid>
if want data bind view model locationtreeview, should use property in view model data bind to. if view model had property named selectedlocationinviewmodel in it, should use data bind to:
<uc:locationtreeview selectedlocation="{binding selectedlocationinviewmodel}" /> i think see problem now... want define properties in usercontrol , data bind them, also data bind properties view model set datacontext. need use relativesource binding that... @ binding paths in these examples:
to data bind properties declared in usercontrol from within usercontrol:
<itemscontrol itemssource="{binding propertyname, relativesource={relativesource ancestortype={x:type yourprefix:yourusercontrol}}}" /> to data bind properties declared in object set datacontext:
<itemscontrol itemssource="{binding propertyname}" />
Comments
Post a Comment