当在一个模型-视图-视图模型架构的WPF应用程序中实现ViewModel时,似乎有两个主要的选择:如何使它可数据化。我已经看到实现使用DependencyProperty属性的视图要绑定,我已经看到ViewModel实现INotifyPropertyChanged代替。

我的问题是,什么时候我应该更喜欢其中一个?有什么性能差异吗?把ViewModel依赖项交给WPF真的是个好主意吗?在做设计决定时,我还需要考虑什么?


当前回答

INotifyPropertyChanged在使用时还允许您在属性的getter和setter的代码中添加更多逻辑。

DependencyProperty例子:

public static DependencyProperty NameProperty = DependencyProperty.Register( "Name", typeof( String), typeof( Customer ) );

public String Name
{
    set { SetValue( NameProperty, value ); }
    get { return ( String ) GetValue( NameProperty ); }
}

在你的getter和setter中——你所能做的就是分别调用SetValue和GetValue, b/c在框架的其他部分,getter/setter不会被调用,而是直接调用SetValue和GetValue,所以你的属性逻辑不会可靠地执行。

使用INotifyPropertyChanged定义一个事件:

public event PropertyChangedEventHandler PropertyChanged;

然后简单地在你的代码的任何地方有任何逻辑,然后调用:

// ...
// Something cool...
// ...

if( this.PropertyChanged != null )
{
    PropertyChanged( this, new PropertyChangedEventArgs( "Name" ) );
}

// More cool stuff that will reliably happen...

它可以在getter/setter中,也可以在其他任何地方。

其他回答

根据WPF性能指南,DependencyObjects肯定比实现INotifyPropertyChanged的POCOs性能更好:

http://msdn.microsoft.com/en-us/library/bb613546.aspx

看起来依赖属性应该用在你创建的控件中,比如按钮。要在XAML中使用属性并使用所有WPF特性,这些属性必须具有依赖属性。

然而,你的ViewModel最好使用INotifyPropertyChanged。使用INotifyPropertyChanged可以让你在需要的时候拥有getter/setter逻辑。

我建议查看Josh Smith的基类版本,它已经实现了INotifyPropertyChanged:

http://joshsmithonwpf.wordpress.com/2007/08/29/a-base-class-which-implements-inotifypropertychanged/

我认为这是如何创建ViewModel的一个很好的例子。

如果你想将属性暴露给其他控件,你必须使用Dependency属性…但祝你好运,因为他们需要一段时间来弄清楚…

INotifyPropertyChanged在使用时还允许您在属性的getter和setter的代码中添加更多逻辑。

DependencyProperty例子:

public static DependencyProperty NameProperty = DependencyProperty.Register( "Name", typeof( String), typeof( Customer ) );

public String Name
{
    set { SetValue( NameProperty, value ); }
    get { return ( String ) GetValue( NameProperty ); }
}

在你的getter和setter中——你所能做的就是分别调用SetValue和GetValue, b/c在框架的其他部分,getter/setter不会被调用,而是直接调用SetValue和GetValue,所以你的属性逻辑不会可靠地执行。

使用INotifyPropertyChanged定义一个事件:

public event PropertyChangedEventHandler PropertyChanged;

然后简单地在你的代码的任何地方有任何逻辑,然后调用:

// ...
// Something cool...
// ...

if( this.PropertyChanged != null )
{
    PropertyChanged( this, new PropertyChangedEventArgs( "Name" ) );
}

// More cool stuff that will reliably happen...

它可以在getter/setter中,也可以在其他任何地方。

依赖属性旨在支持在UI元素上绑定(作为目标),而不是作为数据绑定的源,这就是INotifyProperty的作用。从纯粹的角度来看,你不应该在ViewModels上使用DP。

"In order to be the source of a binding, a property does not need to be a dependency property; you can use any CLR property as a binding source. However, in order to be the target of a binding, the property must be a dependency property. For a one-way or two-way binding to be effective, the source property must support change notifications that propagate to the binding system and thus the target. For custom CLR binding sources, this means that the property must support INotifyPropertyChanged. Collections should support INotifyCollectionChanged."

所有依赖对象都不能被序列化(这可能会妨碍ViewModels和DTO (POCO)的使用。

与WPF相比,Silverlight中的DP有一些不同之处。

http://msdn.microsoft.com/en-us/library/cc221408 (v = VS.95) . aspx

http://msdn.microsoft.com/en-us/library/cc903933 (VS.95) . aspx