Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

c# - When should I use dependency properties in WPF?

When should I use dependency properties in WPF?

They are static, so we save a lot on memory, compared to using .NET properties. 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...

So it seems I should ALWAYS use dependency properties in my projects where I use WPF?

Maybe for some trivial properties of helper classes here and there I could get away with .NET properties...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  1. You know the property is going to be the binding target i.e you are creating a user control/custom control and want property that should be binding driven.

  2. You want automatic property change notifications (coerse and validation also).

  3. We want value inheritance in styles,themes, parent or default value.

  4. 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. Resolving Dependency property value is a burden in itself so making property dependency unnecessarily is not advisable.

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...