Base class DependencyProperty value change in WPF

时光总嘲笑我的痴心妄想 提交于 2019-12-12 08:57:11

问题


I have a DependencyProperty in the ClassA. I derived another ClassB from ClassA. When the value of this property is updated or changed in ClassA, How it can be notified or reflected in the derived ClassB with out adding any additional code in ClassA ?


回答1:


By registering a PropertyChangedCallback for ClassB through DependencyProperty.OverrideMetadata in the derived class:

class ClassB
{
    static ClassB()
    {
        ClassA.SomeValueProperty.OverrideMetadata(
            typeof(ClassB), new PropertyMetadata(SomeValuePropertyChanged);
    }

    private static SomeValuePropertyChanged(
        DependencyObject o, DependencyPropertyChangedArgs e)
    {
        ...
    }
}



回答2:


If you want the property change to be raised in both classes you can add another owner.

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:


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";
    }
}


来源:https://stackoverflow.com/questions/19560215/base-class-dependencyproperty-value-change-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!