INotifyPropertyChanged 与 ViewModel 中的 DependencyProperty

问题描述:

在 Model-View-ViewModel 架构 WPF 应用程序中实现 ViewModel 时,似乎有两个主要选择如何使其可数据绑定.我已经看到将 DependencyProperty 用于视图将绑定的属性的实现,并且我已经看到 ViewModel 实现了 INotifyPropertyChanged.

When implementing the ViewModel in a Model-View-ViewModel architecture WPF application there seem to be two major choices how to make it databindable. I have seen implementations that use DependencyProperty for properties the View is going to bind against and I have seen the ViewModel implementing INotifyPropertyChanged instead.

我的问题是我什么时候应该更喜欢一个?是否有任何性能差异?将 ViewModel 依赖项赋予 WPF 真的是个好主意吗?在做出设计决策时,我还需要考虑什么?

My question is when should I prefer one over the other? Are there any performance differences? Is it really a good idea to give the ViewModel dependencies to WPF? What else do I need to consider when make the design decision?

Kent 写了一篇关于这个主题的有趣博客:查看模型:POCO 与 DependencyObjects.

Kent wrote an interesting blog about this topic: View Models: POCOs versus DependencyObjects.

简短摘要:

  1. 依赖对象未标记为可序列化
  2. DependencyObject 类覆盖并密封了 Equals() 和GetHashCode() 方法
  3. DependencyObject 具有线程关联性——它只能被访问在它所在的线程上已创建

我更喜欢 POCO 方法.可以在此处找到实现 INotifyPropertyChanged 接口的 PresentationModel(又名 ViewModel)的基类:http://compositeextensions.codeplex.com一个>

I prefer the POCO approach. A base class for PresentationModel (aka ViewModel) which implements INotifyPropertyChanged interface can be found here: http://compositeextensions.codeplex.com