c# - WPF usercontrol Twoway binding Dependency Property -
i created dependency property in usercontrol, changes in usercontrol not notified viewmodel
usercontrol
<usercontrol x:class="dpsample.usercontrol1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:ignorable="d" d:designheight="300" d:designwidth="300"> <grid> <textbox x:name="txtname"></textbox> </grid>
usercontrol.cs
/// <summary> /// interaction logic usercontrol1.xaml /// </summary> public partial class usercontrol1 : usercontrol { public usercontrol1() { initializecomponent(); } #region sampleproperty public static readonly dependencyproperty samplepropertyproperty = dependencyproperty.register("sampleproperty", typeof(string), typeof(usercontrol1), new propertymetadata(onsamplepropertychanged)); public string sampleproperty { { return (string)getvalue(samplepropertyproperty); } set { setvalue(samplepropertyproperty, value); } } static void onsamplepropertychanged(dependencyobject obj, dependencypropertychangedeventargs e) { (obj usercontrol1).onsamplepropertychanged(e); } private void onsamplepropertychanged(dependencypropertychangedeventargs e) { string samplepropertynewvalue = (string)e.newvalue; txtname.text = samplepropertynewvalue; } #endregion }
mainwindow
<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:dpsample" x:class="dpsample.mainwindow" title="mainwindow" height="350" width="525"> <grid> <local:usercontrol1 sampleproperty="{binding sampletext,mode=twoway}" horizontalalignment="left" margin="76,89,0,0" verticalalignment="top" width="99"/> <button content="show" horizontalalignment="left" margin="76,125,0,0" verticalalignment="top" width="75" click="button_click"/> </grid>
mainwindow.cs
public mainwindow() { initializecomponent(); this.datacontext = new mainviewmodel(); } private void button_click(object sender, routedeventargs e) { var item = this.datacontext mainviewmodel; messagebox.show(item.sampletext.tostring()); }
mainviewmodel.cs
public class mainviewmodel : notifyviewmodelbase { public mainviewmodel() { this.sampletext = "test"; } private string _sampletext; public string sampletext { { return _sampletext; } set { _sampletext = value; onpropertychanged("sampletext"); } } }
bind textbox.text
property in usercontrol sampleproperty
this:
<textbox text="{binding sampleproperty, relativesource={relativesource ancestortype=usercontrol}}"/>
now remove onsamplepropertychanged
callback.
you might register sampleproperty
bind two-way default this:
public static readonly dependencyproperty samplepropertyproperty = dependencyproperty.register( "sampleproperty", typeof(string), typeof(usercontrol1), new frameworkpropertymetadata( null, frameworkpropertymetadataoptions.bindstwowaybydefault));
Comments
Post a Comment