.net 什么是依赖属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/617312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a dependency property?
提问by e11s
What is a dependency property in .Net (especially in WPF context). What is the difference from the regular property?
.Net 中的依赖属性是什么(尤其是在 WPF 上下文中)。与普通房产有什么区别?
采纳答案by Matt Hamilton
Dependency properties are properties of classes that derive from DependencyObject, and they're special in that rather than simply using a backing field to store their value, they use some helper methods on DependencyObject.
依赖属性是派生自 DependencyObject 的类的属性,它们的特殊之处在于它们不是简单地使用支持字段来存储它们的值,而是在 DependencyObject 上使用一些辅助方法。
The nicest thing about them is that they have all the plumbing for data binding built in. If you bind something to them, they'll notify it when they change.
关于它们的最好的事情是它们内置了数据绑定的所有管道。如果您将某些内容绑定到它们,它们会在更改时通知它。
回答by Jonathan Perry
The only explanation I found helpful and well written is this one: http://www.wpftutorial.net/dependencyproperties.html
我发现有用且写得很好的唯一解释是这个:http: //www.wpftutorial.net/dependencyproperties.html
Basically, DependencyProperties differ from regular properties in that they're not just setters / getters for fields in the class, but they retrieve their actual values dynamically during runtime. The SetValue()method of DPs is pretty straightforward and sets the local value of the property to the value you gave it. However, when you try to GetValue()from a DependencyProperty, it will first look for a local value, if none is present (which is viable in DependencyProperties unlike regular properties) it will continue up the logical UI tree until it will find such value. If the framework has reached the top of the tree without finding any local values, it will then use a predefined default value as the property's value.
基本上,DependencyProperties 与常规属性的不同之处在于它们不仅仅是类中字段的 setter/getter,而且它们在运行时动态地检索它们的实际值。SetValue()DPs的方法非常简单,将属性的本地值设置为您给它的值。但是,当您尝试GetValue()从 DependencyProperty 中查找时,它将首先查找本地值,如果不存在(与常规属性不同,这在 DependencyProperties 中是可行的),它将继续沿逻辑 UI 树向上,直到找到这样的值。如果框架在没有找到任何本地值的情况下到达树的顶部,它将使用预定义的默认值作为属性的值。
This method allows DependencyProperties to consume less memory than regular properties since only values that were explicitly set by the user will be stored locally.
此方法允许 DependencyProperties 比常规属性消耗更少的内存,因为只有用户明确设置的值才会存储在本地。
And, as mentioned above, DependencyProperties also allow us to bind to them in the XAML code and set triggers on them, which on regular properties is not allowed.
而且,如上所述,DependencyProperties 还允许我们在 XAML 代码中绑定到它们并在它们上设置触发器,这在常规属性上是不允许的。
I hope I've managed to clear some of the vagueness :)
我希望我已经设法清除了一些含糊之处:)
回答by Ash M
http://techpunch.wordpress.com/2008/09/25/wpf-wf-what-is-a-dependency-property/provides a good explanation of dependency properties both in the context of WF and WPF.
http://techpunch.wordpress.com/2008/09/25/wpf-wf-what-is-a-dependency-property/在 WF 和 WPF 的上下文中提供了对依赖属性的很好的解释。
An excerpt:
摘录:
Key Point – The Value of Dependency Properties Are Resolved
The ultimate goal of a dependency property, like any property, is to manage state. But unlike normal .Net properties, the local property value is not stored in an instance variable.
Instead, dependency properties are registered with the dependency property framework, and the underlying property value is resolved – meaning the value is determined by the dependency property framework based on rules defined by the property registration.
关键点——依赖属性的值得到解决
与任何属性一样,依赖属性的最终目标是管理状态。但与普通的 .Net 属性不同,本地属性值不存储在实例变量中。
相反,依赖属性在依赖属性框架中注册,并且底层属性值被解析——这意味着该值是由依赖属性框架根据属性注册定义的规则确定的。

