什么是依赖项属性和WPF附加属性之间的区别? [英] What's the difference between a dependency property and an attached property in WPF?
问题描述
什么是(自定义)依赖项属性和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.
这篇关于什么是依赖项属性和WPF附加属性之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!