【问题标题】:Base class DependencyProperty value change in WPFWPF 中的基类 DependencyProperty 值更改
【发布时间】:2013-10-24 08:00:04
【问题描述】:

我在 ClassA 中有一个 DependencyProperty。我从 ClassA 派生了另一个 ClassB。当这个属性的值在 ClassA 中更新或改变时,如何在 ClassA 中不添加任何额外代码的情况下通知或反映到派生的 ClassB 中?

【问题讨论】:

    标签: c# wpf dependency-properties


    【解决方案1】:

    通过在派生类中通过DependencyProperty.OverrideMetadata 为 ClassB 注册 PropertyChangedCallback:

    class ClassB
    {
        static ClassB()
        {
            ClassA.SomeValueProperty.OverrideMetadata(
                typeof(ClassB), new PropertyMetadata(SomeValuePropertyChanged);
        }
    
        private static SomeValuePropertyChanged(
            DependencyObject o, DependencyPropertyChangedArgs e)
        {
            ...
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果您希望在两个类中引发属性更改,您可以添加另一个所有者。

      using System.Windows;
      
      namespace dp
      {
          public class ClassA : DependencyObject
          {
              public string TestProperty
              {
                  get { return (string)GetValue(TestPropertyProperty); }
                  set { SetValue(TestPropertyProperty, value); }
              }
              public static readonly DependencyProperty TestPropertyProperty =
                  DependencyProperty.Register("TestProperty", typeof(string), typeof(ClassA), new PropertyMetadata(null, new PropertyChangedCallback( (s, e)=>
                      {
                      })));
          }
      
          public class ClassB : ClassA
          {
              static ClassB()
              {
                  TestPropertyProperty.AddOwner(typeof(ClassB), new PropertyMetadata((s, e) =>
                      {
                      }));
              }    
          }
      
          public partial class MainWindow : Window
          {
              public ClassB TestClassB
              {
                  get { return (ClassB)GetValue(TestClassBProperty); }
                  set { SetValue(TestClassBProperty, value); }
              }        
              public static readonly DependencyProperty TestClassBProperty =
                  DependencyProperty.Register("TestClassB", typeof(ClassB), typeof(MainWindow), new PropertyMetadata(null));
      
      
              public MainWindow()
              {
                  InitializeComponent();
                  TestClassB = new ClassB();
                  TestClassB.TestProperty = "test";
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        只是一个评论:如果你使用 B 类的 2 个实例,你会得到一个错误

        • _innerException {“PropertyMetadata 已为类型 'ClassB' 注册。”} System.Exception {System.ArgumentException}

        【讨论】:

          【解决方案4】:
          public class ClassA : DependencyObject
          {
              /// <summary>
              /// 
              /// </summary>
              public string PropertyA
              {
                  get { return (string)GetValue(PropertyAProperty); }
                  set { SetValue(PropertyAProperty, value); }
              }
          
              /// <summary>
              /// Identifies the <see cref="PropertyA"/> dependency property.
              /// </summary>
              public static readonly DependencyProperty PropertyAProperty =
              DependencyProperty.Register("PropertyA", typeof(string), typeof(ClassA), new PropertyMetadata("A"));
          }
          
          
          public class ClassB : ClassA
          {
              /// <summary>
              /// 
              /// </summary>
              public string PropertyB
              {
                  get { return (string)GetValue(PropertyBProperty); }
                  set { SetValue(PropertyBProperty, value); }
              }
          
              /// <summary>
              /// Identifies the <see cref="PropertyB"/> dependency property.
              /// </summary>
              public static readonly DependencyProperty PropertyBProperty =
              DependencyProperty.Register("PropertyB", typeof(string), typeof(ClassA), new PropertyMetadata("B"));
          
              public ClassB()
              {
                  ClassA.PropertyAProperty.OverrideMetadata(typeof(ClassB), new PropertyMetadata(AValuePropertyChanged));
              }
          
              private static void AValuePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
              {
                  MessageBox.Show(e.NewValue.ToString());
              }
          }
          
          public partial class MainWindow4 : Window
          {
              /// <summary>
              /// 
              /// </summary>
              public MainWindow4()
              {
                  InitializeComponent();
          
                  this.Reference = new ClassB();
              }
          
              private ClassB Reference { get; set; }
          
              /// <summary>
              /// 
              /// </summary>
              /// <param name="sender"></param>
              /// <param name="e"></param>
              private void Button_Click(object sender, RoutedEventArgs e)
              {
          
          
          
                  this.Reference.PropertyA = "hello";
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2011-01-04
            • 1970-01-01
            • 2017-11-22
            • 1970-01-01
            • 2011-02-20
            • 1970-01-01
            • 2021-09-10
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多