INotifyPropertyChanged 这个接口到底是干什么用得。能否给个实例???
不一定非要mvvm,
INotifyPropertyChanged 是向客户端发出某一属性值已更改的通知。
INotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知。
例如,考虑一个带有名为 FirstName 属性的 Person 对象。若要提供一般性属性更改通知,则 Person 类型实现 INotifyPropertyChanged 接口并在 FirstName 更改时引发 PropertyChanged 事件。
看一下微软的文档吧,有例子
http://msdn.microsoft.com/zh-cn/library/system.componentmodel.inotifypropertychanged.aspx
我自己做用wpf了一个小例子 你可以下载下来 INotifyPropertyChangedDemo.rar
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid Name="gd_Model"> <Label Content="{Binding NumberFirst}" Margin="0,0,211,218"></Label> <Label Content="{Binding NumberSecond}" Margin="0,99,211,107"></Label> <Button Width="80" Height="30" Click="btn_Click" Margin="365,158,58,124"></Button> </Grid> </Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.ComponentModel; namespace WpfApplication1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { PlusModel model_PlusModel = new PlusModel(); public MainWindow() { InitializeComponent(); model_PlusModel.NumberFirst = 5; model_PlusModel.NumberSecond = 5; gd_Model.DataContext = model_PlusModel; } public void btn_Click(object sender, RoutedEventArgs e) { model_PlusModel.NumberFirst++; model_PlusModel.NumberSecond++; } } public class PlusModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public PlusModel() { } /// <summary> /// 第一个加数 /// </summary> private decimal _numberFirst; /// <summary> /// 第二个加数 /// </summary> private decimal _numberSecond; /// <summary> /// 第一个加数 /// </summary> public decimal NumberFirst { get { return _numberFirst; } set { _numberFirst = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("NumberFirst")); } } } /// <summary> /// 第二个加数 /// </summary> public decimal NumberSecond { get { return _numberSecond; } set { _numberSecond = value; } } } }
就是监控属性值的更改,对属性值发生更改时触发一个INotifyPropertyChanged事件。
一楼正解
学习来的!!