[英]When should I use dependency properties in WPF?
When should I use dependency properties in WPF?什么时候应该在 WPF 中使用依赖属性?
They are static, so we save a lot on memory, compared to using .NET properties.它们是静态的,因此与使用 .NET 属性相比,我们可以节省大量内存。 Other gains of using dependency properties over .NET properties are: 1) no need to check thread access 2) prompt a containing element to be rendered etc...
使用依赖属性而不是 .NET 属性的其他好处是:1) 无需检查线程访问 2) 提示要呈现包含元素等...
So it seems I should ALWAYS use dependency properties in my projects where I use WPF?所以看起来我应该总是在我使用 WPF 的项目中使用依赖属性?
Maybe for some trivial properties of helper classes here and there I could get away with .NET properties...也许对于这里和那里的辅助类的一些微不足道的属性,我可以摆脱 .NET 属性......
Dependency Property is a broad concept to explain which might take couple of pages to write.依赖属性是一个广泛的概念,用于解释可能需要几页才能编写的内容。 So just to answer your main question, Dependency property is used where
所以只是为了回答你的主要问题,依赖属性用于
You know the property is going to be the binding target ie you are creating a user control/custom control and want property that should be binding driven.您知道该属性将成为绑定目标,即您正在创建一个用户控件/自定义控件并希望属性应该是绑定驱动的。
You want automatic property change notifications (coerse and validation also).您需要自动属性更改通知(也可以是 coerse 和验证)。
We want value inheritance in styles,themes, parent or default value.我们希望在样式、主题、父级或默认值中继承值。
There is not need to create the properties on Model or ViewModel layer as dependency properties most of the time as that is not going to help much on memory saving front as most of the properties we define in model/VM will have values per instance as they will be constantly changing.大多数情况下不需要在 Model 或 ViewModel 层上创建属性作为依赖属性,因为这对节省内存没有太大帮助,因为我们在模型/VM 中定义的大多数属性将具有每个实例的值,因为它们将不断变化。 Resolving Dependency property value is a burden in itself so making property dependency unnecessarily is not advisable.
解析 Dependency 属性值本身就是一种负担,因此不建议不必要地进行属性依赖。
Thanks谢谢
The main reason to create DependencyProperty
is when you write you own WPF control.创建
DependencyProperty
主要原因是当您编写自己的 WPF 控件时。 DependencyProperties
can be used as binding source and target, and can be animated. DependencyProperties
可以用作绑定源和目标,并且可以设置动画。 The properties of all framework's controls are implemented as DependencyProperty
, that why you can make powerful data binding in XAML.所有框架控件的属性都实现为
DependencyProperty
,这就是为什么您可以在 XAML 中进行强大的数据绑定。
However in most situation, like in with the MVVM pattern, you don't need dependency properties, INotifyPropertyChanged
is enough.但是在大多数情况下,就像在 MVVM 模式中一样,您不需要依赖属性,
INotifyPropertyChanged
就足够了。
The main difference is, that the value of a normal .NET property is read directly from a private member in your class, whereas the value of a DependencyProperty is resolved dynamically when calling the GetValue() method that is inherited from DependencyObject.主要区别在于,普通 .NET 属性的值是直接从类中的私有成员读取的,而 DependencyProperty 的值是在调用从 DependencyObject 继承的 GetValue() 方法时动态解析的。
When you set a value of a dependency property it is not stored in a field of your object, but in a dictionary of keys and values provided by the base class DependencyObject.当您设置依赖属性的值时,它不会存储在对象的字段中,而是存储在基类 DependencyObject 提供的键和值的字典中。 The key of an entry is the name of the property and the value is the value you want to set.
条目的键是属性的名称,值是您要设置的值。
The advantages of dependency properties are依赖属性的优点是
check the below url for more details about the magic behid it查看下面的网址,了解有关隐藏它的魔法的更多详细信息
CLR Property vs. Dependency Property CLR 属性与依赖属性
A CLR property reads directly from the private member of the class. CLR 属性直接从类的私有成员中读取。 The Get() and Set() methods of the class retrieve and store the values of the property.
该类的 Get() 和 Set() 方法检索和存储属性的值。 Whereas when you set a value of a Dependency Property it is not stored in a field of your object, but in a dictionary of keys and values provided by the base class DependencyObject.
而当您设置依赖属性的值时,它并不存储在对象的字段中,而是存储在基类 DependencyObject 提供的键和值的字典中。 The key of an entry is the name of the property and the value is the value you want to set.
条目的键是属性的名称,值是您要设置的值。
Advantages of a Dependency Property Less memory consumption依赖属性的优点 更少的内存消耗
The Dependency Property stores the property only when it is altered or modified.依赖属性仅在更改或修改属性时存储该属性。 Hence a huge amount of memory for fields are free.
因此,大量的字段内存是免费的。
Property value inheritance It means that if no value is set for the property then it will return to the inheritance tree up to where it gets the value.属性值继承这意味着如果没有为属性设置值,那么它将返回到继承树直到它获取值的位置。
Change notification and Data Bindings Whenever a property changes its value it provides notification in the Dependency Property using INotifyPropertyChange and also helps in data binding.更改通知和数据绑定每当属性更改其值时,它都会使用 INotifyPropertyChange 在依赖属性中提供通知,并且还有助于数据绑定。
Participation in animation, styles and templates A Dependency Property can animate, set styles using style setters and even provide templates for the control.参与动画、样式和模板依赖属性可以设置动画、使用样式设置器设置样式,甚至为控件提供模板。
CallBacks Whenever a property is changed you can have a callback invoked.每当更改属性,你可以有一个回调回调调用。
Resources You can define a Resource for the definition of a Dependency Property in XAML.资源您可以为 XAML 中的依赖属性定义定义一个资源。
Overriding Metadata You can define certain behaviours of a Dependency Property using PropertyMetaData.覆盖元数据您可以使用 PropertyMetaData 定义依赖属性的某些行为。 Thus, overriding a metadata from a derived property will not require you to redefine or re-implement the entire property definition.
因此,从派生属性覆盖元数据不需要您重新定义或重新实现整个属性定义。
Perhaps you should take another look at the Dependency Properties Overview page at MSDN.也许您应该再看看 MSDN 上的依赖属性概述页面。
Personally, I would only ever create a DependencyProperty
when I really need to.就个人而言,我只会在真正需要时创建
DependencyProperty
。 For the most part, I use normal CLR properties in data type and view model classes to bind to... this is absolutely fine, as long as I implement the INotifyPropertyChanged
interface.在大多数情况下,我在数据类型和视图模型类中使用普通的 CLR 属性来绑定到......这绝对没问题,只要我实现
INotifyPropertyChanged
接口。
So for all of my usual data bindings, I use normal CLR properties.所以对于我所有常用的数据绑定,我使用普通的 CLR 属性。 I only declare a
Dependency Property
in order to provide some added functionality in a UserControl
.我只声明了一个
Dependency Property
,以便在UserControl
提供一些附加功能。
Dependency properties are used when you want data binding in a UserControl
, and is the standard method of data binding for the WPF Framework controls.依赖属性用于在
UserControl
数据绑定,并且是 WPF 框架控件的标准数据绑定方法。 DPs have slightly better binding performance , and everything is provided to you when inside a UserControl
to implement them. DP 的绑定性能稍好一些,并且在
UserControl
为您提供所有内容以实现它们。
Otherwise, you typically use INotifyPropertyChanged
for binding elsewhere, since it's easier to implement in stand-alone classes and has less overhead.否则,您通常使用
INotifyPropertyChanged
在别处进行绑定,因为它更容易在独立类中实现并且开销更少。 As far as your original assumptions:至于你原来的假设:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.