WPF自定义DependencyProperty通知更改 [英] WPF custom DependencyProperty notify changes

查看:351
本文介绍了WPF自定义DependencyProperty通知更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为MyComponent的类,它具有一个名为BackgroundProperty的DependencyProperty。

I have a class called MyComponent and it has a DependencyProperty caled BackgroundProperty.

public class MyComponent
{
    public MyBackground Background
    {
        get { return (MyBackground)GetValue(BackgroundProperty); }
        set { SetValue(BackgroundProperty, value); }
    }
    public static readonly DependencyProperty BackgroundProperty =
        DependencyProperty.Register("Background", typeof(MyBackground),
            typeof(MyComponent), new FrameworkPropertyMetadata(default(MyBackground), new PropertyChangedCallback(OnPropertyChanged)));
}

MyBackground是一个从DependencyObject派生的类,它具有一些DependencyProperties。

MyBackground is a class that derives from DependencyObject and it has some DependencyProperties.

public class MyBackground : DependencyObject
{
    public Color BaseColor
    {
        set { SetValue(BaseColorProperty, value); }
        get { return (Color)GetValue(BaseColorProperty); }
    }
    public static readonly DependencyProperty BaseColorProperty =
        DependencyProperty.Register("BaseColor", typeof(Color),
            typeof(MyBackground ), new UIPropertyMetadata(Colors.White));

    [...]
}

现在,什么我想要的是更改MyBackground的属性时,将通知MyComponent MyBackground发生变化的MyComponent以及要调用名为OnPropertyChanged的PropertyChangedCallback。

Now, what I want is when a property from MyBackground is changed, MyComponent to be notified that MyBackground has changed and the PropertyChangedCallback named OnPropertyChanged to be called.

推荐答案

一种执行您描述的方法的方法是从派生Freezable 而不是DependencyObject。当Freezable的属性更改时,将调用任何引用Freezable的DO的PropertyChangedCallback,以便对MyComponent的Background属性进行回调。在这种情况下,e.OldValue和e.NewValue将是相同的引用。 WPF内部在事件args上有一些标志,指示它是子对象更改。

One way to do what you describe would be to derive from Freezable instead of DependencyObject. When a property of a Freezable changes the PropertyChangedCallback for any DO referencing that Freezable will be invoked so the callback for the Background property of your MyComponent. In that case the e.OldValue and e.NewValue will be the same reference. Internally WPF has some flag on the event args that indicates that it is a subobject change.

这是框架为画笔之类的功能,因此,如果说改变了SolidColorBrush的Color属性,则该元素可以无效。如果一个对象永远不会被更改(或者您想使其成为线程安全的),则可以冻结该对象(即使其变为不可变的)。

This is what the framework does for things like brushes so that an element can be invalidated if say the Color property of a SolidColorBrush is changed. If an object will never be changed (or you want to make it thread safe) then one can freezing the object (i.e. making it immutable).

BTW我可能会避免使用Background作为属性的名称。大多数开发人员会假定该类型为Brush,因为该框架在其几个元素(例如,控件,边框)上对该命名属性使用。

BTW I would probably avoid using Background as the name of the property. Most developers will assume that is of type Brush as that is what the framework uses for that named property on several of its elements (e.g. control, border).

这篇关于WPF自定义DependencyProperty通知更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆