在ViewModel中INotifyPropertyChanged vs. 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.

我的问题是我何时应该选择一个?有没有性能差异?给WPF的ViewModel依赖是一个好主意吗?

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?

肯特写了一篇关于这个主题的有趣的博客:查看模型:POCOs与DependencyObjects

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

简短摘要


  1. DependencyObjects没有标记为
    可序列化

  2. DependencyObject类覆盖并密封Equals()和
    GetHashCode()方法

  3. DependencyObject具有线程关联性 - 只能被访问

    创建的线程上

我更喜欢POCO方法。 PresentationModel(又名ViewModel)的基类实现INotifyPropertyChanged接口可以在这里找到: 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