c# - User Control binding not working for nested property -
i have user control called informationcontrol. using in window, this:
<general:informationcontrol grid.row="0" timetostart="{binding timetostart}" poor="{binding path=mine.poor}" status="{binding path=mine.minestatus}"/>
the binding on timetostart dependency property working fine, when use path=object.property
doesn't seem work.
however, when use path=object.property
on regular control (i.e. not usercontrol) works fine:
<itemscontrol grid.row="2" name="items" itemssource="{binding path=mine.participants}">
i'm breaking in getter mine, , in binding logging saying null, has been set. fact it's trying property in first place makes me feel binding correct can't work out why it's not working.
do need different make sure binding nested properties works on usercontrol dependency properties ?
code behind information control, set break point @ debug.print("newvalue...")
namespace wpfstackoverflow { /// <summary> /// interaction logic informationcontrol.xaml /// </summary> public partial class informationcontrol : usercontrol { public static readonly dependencyproperty timetostartproperty; static informationcontrol() { //frameworkpropertymetadata metadata = new frameworkpropertymetadata(""); timetostartproperty = dependencyproperty.register("timetostart", typeof(string), typeof(informationcontrol), new uipropertymetadata(string.empty, usernamepropertychangedcallback)); } public string timetostart { { return (string)getvalue(timetostartproperty); } set { setvalue(timetostartproperty, value); } } public informationcontrol() { initializecomponent(); string temp = timetostart; } private static void usernamepropertychangedcallback(dependencyobject d, dependencypropertychangedeventargs e) { debug.print("oldvalue: {0}", e.oldvalue); debug.print("newvalue: {0}", e.newvalue); } } }
main window xaml:
<window x:class="wpfstackoverflow.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:wpfstackoverflow" title="mainwindow" height="350" width="525"> <grid> <local:informationcontrol timetostart="{binding mine.name}" /> </grid> </window>
main window code behind:
namespace wpfstackoverflow { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { public mainwindow() { initializecomponent(); parent p = new parent(); p.mine = new mine(); p.mine.name = "hello world"; this.datacontext = p; } } }
parent class:
namespace wpfstackoverflow { public class parent:inotifypropertychanged { private mine _mine; public mine mine { { return _mine; } set { _mine = value; notifypropertychanged(); } } private void notifypropertychanged([callermembername] string propertyname = "") { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } public event propertychangedeventhandler propertychanged; } public class mine : inotifypropertychanged { private string _name; public string name { { return _name; } set { _name = value; notifypropertychanged(); } } private void notifypropertychanged([callermembername] string propertyname = "") { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } public event propertychangedeventhandler propertychanged; } }
i have no idea trying accomplish, if @ example, getting mine.name works in user control if set break point in informationcontrol's dependency property change callback. note setter never called because clr bypass setter , call setvalue directly.
Comments
Post a Comment