开发者

Dependency Property has a lots of advantages over the normal CLR properties How? [duplicate]

This 开发者_如何学Pythonquestion already has answers here: What is the difference between Property and Dependency Property (5 answers) Closed 8 years ago.

why microsoft says Dependency Property has a lots of advantages over the normal CLR properties? Is it possible to do all thing by normal properties which dependency property can do ?


While the answers in Kishore Kumar's link pretty much nail it, I want to add one thing: there isn't really any such thing as a "normal property". Properties are really just method calls in disguise, and as such you can implement them in any way you see fit.

C# 3 did add auto-properties, which implement properties on top of a field, and using a field as backing store for a property is certainly pretty common. While you could go as far as to call that "normal", there's nothing special about that kind of property. As far as the CLR is concerned, a DP is a normal property: it provides a getter and a setter in the normal way, meaning that any language that understands CLR properties can use the normal property syntax when accessing the property.

It's not as if a simple field-backed property is the only common implementation. Lots of classes do extra work in their property accessors to support INotifyPropertyChanged for example. But it'd be odd to say that these are not "normal" properties.

So in a sense, a DP is just one of the very many different ways in which you could implement a normal property.

So to answer the question: yes, it is "possible to do all thing by normal properties which dependency property can do" because you just use a dependency property as the implementation for your normal property.

Now if you wanted your property to offer all the same behaviour that a DP offers without actually using the DP system in your implementation...that'd be a lot of work. And in some cases, it wouldn't even be possible: e.g., if you want a property that can participate in a TemplateBinding inside a ControlTemplate, then as far as I know, it absolutely has to be a DP. But if you ignore DP features that are highly specialized to WPF, then yes, you could recreate equivalent behaviour. After all, WPF is just code. So if WPF can do something, so can you - you can write your own version of WPF given enough time and resources.


Basically, with DP's you get to have a lot of events normal properties dont have. I know it is tedious to register them, but they are pretty flexible :)

More info in the Kishore's link :p


dependency properties are very difficult to unit test, so should not be used in ViewModels (as some people do), but only in WPF UI controls

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜