Silverlight 之 INotifyPropertyChanged

NotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知。

例如,考虑一个带有名为 FirstName 属性的 Person 对象。 若要提供一般性属性更改通知,则 Person 类型实现 INotifyPropertyChanged 接口并在 FirstName 更改时引发 PropertyChanged 事件。

若要在将客户端与数据源进行绑定时发出更改通知,则绑定类型应具有下列任一功能:

  • 实现 INotifyPropertyChanged 接口(首选)。

  • 为绑定类型的每个属性提供更改事件。

不执行上述这两个功能。

 实例代码:

View Code
 public class BookInfo : ViewModel
{
private string _name;
private int _count;
private DateTime _publishDate;
public string Name
{
get
{
return this._name;
}

set
{
this._name = value;
this.RaisePropertyChanged("Name");
}
}

public int Count
{
get
{
return this._count;
}

set
{
this._count = value;
this.RaisePropertyChanged("Count");
}
}

public DateTime PublishDate
{
get
{
return this._publishDate;
}

set
{
this._publishDate = value;
this.RaisePropertyChanged("PublishDate");
}
}

public class ViewModel : IViewModel
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

protected void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}

/// <summary>
/// 视图模块接口
/// </summary>
/// <!--2010年8月14日-->
public interface IViewModel : System.ComponentModel.INotifyPropertyChanged
{
}



posted @ 2011-12-08 14:17  好佳伙  阅读(299)  评论(0编辑  收藏  举报