我什么时候应该使用WPF依赖属性? [英] When should I use dependency properties in WPF?

查看:901
本文介绍了我什么时候应该使用WPF依赖属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我应该使用WPF依赖属性?

When should I use dependency properties in WPF?

他们是静态的,所以我们节省内存很多,相对于使用.NET属性。
在.NET属性使用依赖属性的其他收益:
1)无需检查线程访问
2)提示包含的元素要呈现
等...

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...

这样看来,我应该永远在我的项目,我用WPF使用依赖属性?

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

这里的辅助类的属性,有我可以
逃脱.NET属性...

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

推荐答案

依赖项属性一个广泛的概念解释这可能需要几页来写。所以刚才回答你的问题。依赖关系属性用在

Dependency Property is a broad concept explaining which might take couple of pages to write. So just to answer your question. Dependancy property is used where


  1. 您知道属性将成为约束性指标,即你所创建的用户控件/自定义控件并希望应该绑定驱动属性。

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

您想自动属性更改通知(coerse和验证也)。

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

我们希望在风格,主题,父母或默认值值继承。

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

有没有需要创建模型的属性或视图模型层的大部分时间作为依赖属性没有什么帮助记忆保存前,因为大多数我们模型中定义的属性/ VM将每个实例的值,因为它们会不断变化。解决依赖属性值本身就是一种负担,使得房地产的依赖不必要地是不可取的。

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 unneccessarily is not advisable.

感谢

这篇关于我什么时候应该使用WPF依赖属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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