【问题标题】:Silverlight 4 Usercontrol Property fails to receive PropertyChanged From Parent ControlSilverlight 4 用户控件属性无法从父控件接收 PropertyChanged
【发布时间】:2011-03-03 13:22:08
【问题描述】:

我设置了一个简单的 Silverlight 4 控件,它应该基于公共属性切换两个文本框的可见性。我将控件添加到视图并将控件属性的数据绑定设置为父视图的视图模型的属性。 当父视图模型的属性发生更改时,用户控件中不会发生任何事情。尽管已绑定,但 OnPropertyChanged 似乎对用户控件的绑定属性不感兴趣。下面是用户控件的代码。

<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”发生变化时,用户控件中什么也没有发生,这让我发疯:) 有任何想法吗?谢谢。

【问题讨论】:

  • 当我在 local:EAPPasswordBox 中设置 ShowText="False" 时,它会进入 ShowText 属性,当我此时通过 Visual Studio 检查 DataContext 时,它说它为空。也许这有帮助。

标签: silverlight user-controls dependency-properties


【解决方案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(); } }

希望对你有帮助。

【讨论】:

  • 效果很好,谢谢,现在我必须弄清楚如何在用户控件的那些文本框中显示父视图模型的验证错误消息...
【解决方案2】:

OnShowTextPropertyChanged 处理程序的属性设置器中实现您所做的。 setter 只会用于初始化绑定。

【讨论】:

    猜你喜欢
    • 2012-07-18
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多