什么是依赖项属性和WPF附加属性之间的区别?属性、区别、WPF

2023-09-02 01:40:23 作者:贱痞@

什么是(自定义)依赖项属性和WPF附加属性之间的区别?什么是用于各个?如何在实现一般有什么不同?

What's the difference between a (custom) dependency property and an attached property in WPF? What are the uses for each? How do the implementations typically differ?

推荐答案

附加属性是一种类型的依赖属性。所不同的是在如何使用它们。

Attached properties are a type of dependency property. The difference is in how they're used.

通过附加属性,该属性是一个类,它是不是它正在被使用的同一类中定义。这通常用于布局。最好的例子就是Panel.ZIndex或Grid.Row - 应用此对照组(如:按钮),但它实际上是在面板或网格定义。该物业被附加到该按钮的实例。

With an attached property, the property is defined on a class that isn't the same class for which it's being used. This is usually used for layout. Good examples are Panel.ZIndex or Grid.Row - you apply this to a control (ie: Button), but it's actually defined in Panel or Grid. The property is "attached" to the button's instance.

这允许容器中,例如,以创建可以在任何的UIElement使用性质。

This allows a container, for example, to create properties that can be used on any UIelement.

对于实现差异 - 它基本上是使用注册与RegisterAttached当你定义的属性只是一个问题

As for implementation differences - it's basically just a matter of using Register vs. RegisterAttached when you define the property.