Dependency Property如何告诉对象要应用? [英] How Dependency Property tell the object to apply to?

查看:89
本文介绍了Dependency Property如何告诉对象要应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我是这个概念的新手,所以我可能会问一些非常基本的问题。)

(I am totally new to this concept so I may be asking very basic questions.)

依赖项属性已在以下代码中注册:

A dependency property is registered with the code below:

public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata);

从逻辑上讲,它只将属性名称与所有者类型相关联。

Logically, it did nothing but associate a property name with the owner type.

因此,如果我有多个所有者类型的实例,并且每个实例将DP设置为不同的值。

So if I have multiple instances of the owner type, and each instance set the DP to different values.

如何存储这些值?

我从这里了解到附加财产: http:// wpftutorial.net/DependencyProperties.html

I read about the Attached Property from here: http://wpftutorial.net/DependencyProperties.html


附加属性

Attached Properties

附加属性是一种特殊的DependencyProperties。它们
允许您将值附加到对该值一无所知的对象。

Attached properties are a special kind of DependencyProperties. They allow you to attach a value to an object that does not know anything about this value.

此概念的一个很好的例子是布局面板。每个布局面板
需要不同的数据以对齐其子元素。 Canvas需要Top
和Left,DockPanel需要Dock等。由于您可以编写自己的
布局面板,因此列表是无限的。因此,您看到
不可能在所有WPF控件上都具有所有这些属性。

A good example for this concept are layout panels. Each layout panel needs different data to align its child elements. The Canvas needs Top and Left, The DockPanel needs Dock, etc. Since you can write your own layout panel, the list is infinite. So you see, it's not possible to have all those properties on all WPF controls.

解决方案是附加属性。它们由控件
定义,该控件需要特定上下文中另一个控件的数据。以
为例,该元素由父级布局面板对齐。

The solution are attached properties. They are defined by the control that needs the data from another control in a specific context. For example an element that is aligned by a parent layout panel.

因此在以下代码段中:

<Canvas>
    <Button Canvas.Top="20" Canvas.Left="20" Content="Click me!"/>
    <Button Canvas.Top="40" Canvas.Left="20" Content="Click me!"/>
</Canvas>

显然,我们无法提供所有对齐属性,例如 Top 按钮。因此,Canvas定义了此类属性,并将它们 附加添加到Button控件。

Apparently we cannot give all the align properties such as Top, Left to Button. So Canvas defines such properties and they are "attached" to Button control.

当Canvas.Top被指定为属性的属性时XAML中的按钮,它将调用在Canvas类型中定义的SetTop()方法。然后将Button作为元素参数传入。
我认为这是Canvas知道哪个Button使用哪个Top值的方法。

When Canvas.Top is specified as an "attribute" of the Button in XAML, it will invoke the SetTop() method which is defined in the Canvas type. And the Button is passed in as the element argument. I think that's how Canvas knows which Button use which Top value.

public static void SetTop(UIElement element, double length);

但是我不明白为什么附加财产必须是从属财产? / strong>它们之间有什么联系?

But I don't see why the Attached Property has to be a Dependency Property? What's the connection between them?

谢谢!

推荐答案

通常,当我们定义 DependencyProperty 时,我们还定义了一个CLR'包装器',使我们能够在其中使用 DependencyProperty 代码:

Usually when we define a DependencyProperty, we also define a CLR 'wrapper' that enables us to use the DependencyProperty in code:

public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
    "Items", typeof(ObservableCollection<string>), typeof(MainWindow), 
     new UIPropertyMetadata(new ObservableCollection<string>()));

public ObservableCollection<string> Items
{
    get { return (ObservableCollection<string>)GetValue(ItemsProperty); }
    set { SetValue(ItemsProperty, value); }
}

在这里您可以看到 GetValue SetValue 方法。我们可以在 Window 和/或 UserControl 中访问这些方法,因为它们都扩展了 DependencyObject 类。您还可以看到此处的 Items 属性是 not static ...这是只是 DependencyProperty 的定义,即静态

Here you can see the GetValue and SetValue methods that @Clemens was talking about. We get access to these methods in a Window and/or UserControl because they both extend the DependencyObject class. You can also see that the Items property here is not static... it is just the definition of the DependencyProperty that is static.

更新>>>

为什么附加财产必须是 DependencyProperty 是因为在.NET中,它们只是...它们的设计只是这样。一个更好的问题可能是,附加财产从 DependencyProperty 中获得什么好处?

There's not really much point in asking why does an Attached Property have to be a DependencyProperty? because in .NET, they just are... they were just designed like that. A better question might be, what benefit does an Attached Property get from being a DependencyProperty?

答案与询问财产从 DependencyProperty 有什么好处?的主要好处是:这些属性可以用于 Binding s, Style s, Animation s和资源等。在WSDN开发人员的MSDN上,两个非常重要的页面(已链接到评论中)可以找到更多详细信息:

The answer to that would be the same as if asked what benefit does a property get from being a DependencyProperty? The main benefits being that these properties can be used in Bindings, Styles, Animations and Resources among other things. More details can be found from the (already linked to in the comments) two very important pages on MSDN for any WPF developers:

依赖项属性概述

附加属性概述

这篇关于Dependency Property如何告诉对象要应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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