目标属性必须是依赖项属性-为什么? [英] target property must be a dependency property - why?

查看:69
本文介绍了目标属性必须是依赖项属性-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解依赖项属性在WPF中起主要作用.但是,我没有得到限制的原因,即在绑定中,目标属性必须是依赖项属性.为什么不能是任何财产?

I understand that dependencies properties serve a major purpose in WPF. However I do not get the reasons behind the restriction that in binding, the target property must be a dependency property. Why can't it be any property?

推荐答案

如果您有兴趣,可以打开Reflector并查看与框架中的依赖项属性和绑定系统有关的一些代码.有许多棘手的事情在进行,以允许对属性路径进行健壮,高性能的解析,并传播对依赖项属性的更改.拥有标准的基础架构还可以管理更复杂的用例,例如从多个来源更新依赖项属性并解决优先级.例如,在为通过样式设置默认属性的属性设置动画时,经常会出现这种情况.

If you're interested, you might open up Reflector and take a look at some of the code related to dependency properties and the binding system in the framework. There is a lot of tricky stuff going on to allow robust, performant resolution of property paths and propagation of changes to dependency properties. Having a standard infrastructure also allows for management of more complex use cases, such as updating a dependency property from more than one source and resolving the priorities. This comes up frequently when animating a property that has its default set by a style, for example.

具有依赖项属性的另一个好处是,它们在内部封装了许多行为(例如通知,验证和强制),这意味着,如果看到依赖项属性,则肯定会支持某些行为.这与INotifyPropertyChanged相反,在INotifyPropertyChanged中,类实现者可能支持也可能不支持所宣传的接口.这意味着类开发人员的工作量会减少.

The other nice thing with dependency properties is that they internally encapsulate a lot of behavior (such as notification, validation and coercion), which means if you see a dependency property, you know that certain behavior will definitely be supported. This is in contrast with INotifyPropertyChanged, where the class implementer may or may not be supporting the interface as advertised. This means less work for class developers.

这篇关于目标属性必须是依赖项属性-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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