2011-03-03 48 views
0

我已经设置了一个简单的Silverlight 4控件,它应该根据公共属性切换两个文本框的可见性。我将控件添加到视图并将控件属性的数据绑定设置为父视图的视图模型的属性。 当父视图模型的属性发生更改时,usercontrol中没有任何反应。虽然它是绑定的,但OnPropertyChanged似乎并不关心用户控件的绑定属性。以下是用户控件的代码。Silverlight 4用户控件属性无法从父控件接收PropertyChanged

<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 
    { 

     get { 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 
    { 
     get { return _PwdBoxVisibility; } 
     set 
     { 
      _PwdBoxVisibility = value; NotifyPropertyChanged("PwdBoxVisibility"); 
     } 
    } 

    private Visibility _TxtBoxVisibility; 

    public Visibility TxtBoxVisibility 
    { 
     get { 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)); 
     } 
    } 

} 

这是我如何使用它在我父视图:

<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 
    { 
     get 
     { 
      return _showPassword; 
     } 
     set 
     { 
      _showPassword = value; 
      RaisePropertyChanged("showPassword"); 
     } 
    } 

当“showPassword”父视图的视图模型的变化,没有在用户控制情况,它让我疯狂:) 任何想法?谢谢。

+0

当我在本地:EAPPasswordBox中设置ShowText =“False”时,它进入ShowText属性,当我通过Visual Studio检查DataContext时,它说它是空的。也许这有帮助。 – hoetz 2011-03-03 13:43:22

回答

1

绑定依赖属性的更新不会与属性的普通get/set访问器发生,而是出现在幕后。因此,在值更改时拦截的唯一方法是在创建依赖项属性时在PropertyMetadata中提供DependencyPropertyChangedEventHandler。

如下:

 

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 as EAPPasswordBox; 

    if (passwordBox != null) 
    { 
     passwordBox.SetVisibilityOfTextBoxes(); 
    } 
} 
 

希望它能帮助。

+0

非常感谢你,现在我必须弄清楚如何在用户控件的文本框中显示父视图模型的验证错误消息... – hoetz 2011-03-03 15:45:11

0

OnShowTextPropertyChanged处理程序中执行您在属性设置器中所做的操作。 setter将仅用于初始化绑定。

相关问题