【问题标题】:DependencyProperties not firing callbacks as expected in UserControlDependencyProperties 未按预期在 UserControl 中触发回调
【发布时间】:2012-10-08 16:41:37
【问题描述】:

我有一个非常简单的DependencyProperty 示例,我已在 UserControl 上注册,但它并没有按预期工作。

我将此属性绑定到 MainWindow 中的一个 DP(称为 Test),它似乎在每次更改时都会触发 OnPropertyChanged 事件,但我的 UserControl 中的 DependencyProperty 似乎只是此绑定的目标在第一次更改此属性时收到通知。

这是我在代码中尝试做的事情:

我的用户控件:

public partial class UserControl1 : UserControl
{
    public static readonly DependencyProperty ShowCategoriesProperty = 
        DependencyProperty.Register("ShowCategories", typeof(bool), typeof(UserControl1), new FrameworkPropertyMetadata(false, OnShowsCategoriesChanged));

    public UserControl1()
    {
        InitializeComponent();
    }

    public bool ShowCategories
    {
        get
        {
            return (bool)GetValue(ShowCategoriesProperty);
        }
        set
        {
            SetValue(ShowCategoriesProperty, value);
        }
    }

    private static void OnShowsCategoriesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //this callback is only ever hit once when I expect 3 hits...

        ((UserControl1)d).ShowCategories = (bool)e.NewValue;
    }
}

MainWindow.xaml.cs

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();

        Test = true;  //this call changes default value of our DP and OnShowsCategoriesChanged is called correctly in my UserControl

        Test = false;  //these following calls do nothing!! (here is where my issue is)
        Test = true;
    }

    private bool test;
    public bool Test
    {
        get { return test; }
        set
        {
            test = value;
            OnPropertyChanged("Test");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string property)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(property));
        }
    }
}

MainWindow.xaml

<Window x:Class="Binding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:uc="clr-namespace:Binding"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"        
    Title="MainWindow" Height="350" Width="525">
  <Grid>
    <uc:UserControl1 ShowCategories="{Binding Test}" />
  </Grid>
</Window>

【问题讨论】:

    标签: c# wpf binding user-controls dependency-properties


    【解决方案1】:

    问题在于您将 ShowCategories 值设置为自身:

    ((UserControl1)d).ShowCategories = (bool)e.NewValue;
    

    这一行没有用,因为ShowCategories 的值已经被修改了。这就是为什么你首先在属性更改回调中。乍一看,这可以看作是无操作:毕竟,您只是将属性值设置为其当前值,在 WPF 中不会引发任何更改。

    但是,由于绑定不是双向的,因此更改属性值会覆盖绑定。这就是为什么不再引发回调的原因。只需删除回调中的分配即可。

    【讨论】:

    • 不错的收获。我什至没有注意回调的作用。
    【解决方案2】:

    将绑定模式设为TwoWay

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多