WPF依赖属性:为什么我需要指定一个所有者类型? [英] WPF Dependency Properties: Why do I need to specify an Owner Type?

查看:158
本文介绍了WPF依赖属性:为什么我需要指定一个所有者类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我如何注册的DependencyProperty

    public static readonly DependencyProperty UserProperty = 
        DependencyProperty.Register("User", typeof (User), 
             typeof (NewOnlineUserNotifier));                                                                                                                 


    public User User
    {
        get
        {
            return (User)GetValue(UserProperty);
        }
        set
        {
            SetValue(UserProperty, value);
        }
    }



的第三个参数 DependencyProperty.Register 方法需要您指定的依赖项属性存在于(在这种情况下,控制的类型,我的用户控件名为 NewOnlineUserNotifier )。

The third parameter of the DependencyProperty.Register method requires you to specify the type of the Control where the Dependency Property resides in (in this case, my User Control is called NewOnlineUserNotifier).

我的问题是,为什么你居然指定所有者的类型,如果你指定一个不同的类型比实际所有者,会发生什么?

推荐答案

这是你调用注册方法从不是财产的实际所有者的类型,因此,您可以'T指定比实际拥有者不同的类型,因为你指定类型的的实际拥有者。

The type that you call the Register method from is not the de facto owner of the property, therefore you can't specify a different type than the actual owner since the type you specify is the actual owner.

这是例子,这可能是有用的当你创建一个包含其他控件的自定义控件。此前的WinForms,如果你有一些额外的信息,这只是该容器是有用的,但是语义属于孩子,那么你可以做的最好的是发生在举行,所有的标签性质的信息。这种去除类型安全和你们俩从来没有确保其他类不会尝试的东西存储在标签别的。现在用WPF依赖属性允许你绑值对象,而无需持有该值的对象本身。一个简单的例子:

An example where this may be useful is when you create a custom control that contains other controls. Previously with WinForms if you had some extra information that was only useful to that container, but semantically belonged to the child, then the best you could do was place that information in the hold-all "Tag" property. This both removed type safety and you were never sure that another class wouldn't try and store something else in the tag. Now with WPF dependency properties allow you to tie values to objects without the object itself needing to hold the value. A trivial example:

public class ButtonContainer : Control
{
    public Button ChildButton { get; set; }

    public static readonly DependencyProperty FirstOwnerProperty =
    DependencyProperty.Register("FirstOwner", typeof(ButtonContainer),
         typeof(Button));

    public ButtonContainer()
    {
        ChildButton = new Button();
        ChildButton.SetValue(FirstOwnerProperty, this);
    }

}

现在该按钮有一个额外的属性这不仅使ButtonContainer的范围内感,只能在ButtonContainer的范围内访问 - 就像一个类型安全,封装标签

Now the button has an extra property that only makes sense within the context of the ButtonContainer and can only be accessed within the context of the ButtonContainer - like a typesafe, encapsulated Tag.

使用新的类,如下所示

ButtonContainer container1 = new ButtonContainer();

ButtonContainer container2 = new ButtonContainer();
container2.ChildButton = container1.ChildButton;



由于ChildButton从一个容器移动到另一个其FirstOwnerProperty的值与行进,就好像它是Button类的真正成员。 container2的可以调用ChildButton.GetValue(FirstOwnerProperty),并找出哪些ButtonContainer最初创建按钮(为什么它可能想这样做是留给作为练习读者...)。所有这一切都可以无需子类化按钮,一个狭窄的专业。

As the ChildButton is moved from one container to another the value of its FirstOwnerProperty travels with it as though it was a real member of the Button class. Container2 can call ChildButton.GetValue(FirstOwnerProperty) and find out which ButtonContainer originally created the button (why it might want to do this is left as an exercise for the reader...). All of this is possible without the need to subclass the button to a narrow speciality.

这篇关于WPF依赖属性:为什么我需要指定一个所有者类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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