Silverlight 4 Usercontrol Property fails to receive PropertyChanged From Parent Control -
i've set simple silverlight 4 control supposed switch visibility of 2 textboxes based on public property. add control view , set databinding of control's property property of parent view's viewmodel. when change in parent viewmodel's property occurs, nothing happens in usercontrol. although it's bound, onpropertychanged doesnt seem interest bound property of user control. below code of user control.
<usercontrol x:class="controls.eappasswordbox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" d:designheight="300" d:designwidth="400" x:name="_root" > <grid x:name="layoutroot" background="white"> <stackpanel horizontalalignment="stretch" verticalalignment="top"> <passwordbox x:name="pwdbox" /> <textbox x:name="txtbox" /> </stackpanel> </grid>
public partial class eappasswordbox : usercontrol, inotifypropertychanged { public bool showtext { { return (bool)getvalue(showtextproperty); } set { setvalue(showtextproperty, value); if (value == true) { this.pwdbox.visibility = system.windows.visibility.collapsed; this.txtbox.visibility = system.windows.visibility.visible; } else { this.pwdbox.visibility = system.windows.visibility.collapsed; this.txtbox.visibility = system.windows.visibility.visible; } } } private visibility _pwdboxvisibility; public visibility pwdboxvisibility { { return _pwdboxvisibility; } set { _pwdboxvisibility = value; notifypropertychanged("pwdboxvisibility"); } } private visibility _txtboxvisibility; public visibility txtboxvisibility { { return _txtboxvisibility; } set { _txtboxvisibility = value; notifypropertychanged("txtboxvisibility"); } } public static readonly dependencyproperty showtextproperty = dependencyproperty.register("showtext", typeof(bool), typeof(eappasswordbox),null); public eappasswordbox() { initializecomponent(); } private static void onshowtextpropertychanged(dependencyobject d, dependencypropertychangedeventargs e) { } public event propertychangedeventhandler propertychanged; private void notifypropertychanged(string info) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(info)); } } }
here how use in parent view:
<local:eappasswordbox x:name="pwdbox" grid.column="1" grid.row="0" grid.columnspan="2" showtext="{binding showpassword, mode=twoway}"></local:eappasswordbox> private bool _showpassword; public bool showpassword { { return _showpassword; } set { _showpassword = value; raisepropertychanged("showpassword"); } }
when "showpassword" in parent view's viewmodel changes, nothing happens in user control, , it's driving me crazy :) ideas? thank you.
updates bound dependency properties don't occur normal get/set accessors of property behind scenes. such way intercept when value changed provider dependencypropertychangedeventhandler in propertymetadata when create dependency property.
as follows:
public static readonly dependencyproperty showtextproperty = dependencyproperty.register("showtext", typeof(bool), typeof(eappasswordbox), new propertymetadata(showtextpropertychanged)); private static void showtextpropertychanged(dependencyobject sender, dependencypropertychangedeventargs e) { eappasswordbox passwordbox = sender eappasswordbox; if (passwordbox != null) { passwordbox.setvisibilityoftextboxes(); } }
hope helps.
Comments
Post a Comment