INotifyPropertyChanged interface is used to send a notification that a property value has been changed to the client (usually the client that executes binding.
When a property value of the bound data source changes, it notifies the client and updates the interface data. instead of writing a lot of complex code to update the interface data, the method can be concise and clear. INotifyPropertyChanged is indeed a powerful interface.
First, you need to understand the following terms about Silverlight 2.0 Data Binding:
Binding-link the attributes of the bound target object with the data source
Source-bound Data Source
Mode-the bound Data stream direction [System. Windows. Data. BindingMode enumeration]
BindingMode. OneTime-one binding. Update the attributes of the bound target object at one time when creating the binding.
BindingMode. OneWay-one-way binding (default ). Changing the data source will automatically notify the attributes of the bound target object.
BindingMode. TwoWay-bidirectional binding. Mutual notification is triggered when the attribute values of the data source or bound target object change. Obviously, two-way binding is required for data verification.
INotifyPropertyChanged-notifies the client that a property value has been changed
IValueConverter-value conversion API, which converts a value of one type to a value of another type. It provides a way to apply custom logic to binding
The following is a class for implementing the INotifyPropertyChanged interface.Public class User: INotifyPropertyChanged // class
{
Private string _ Name;
Public string Name
{
Get {return this. _ Name ;}
Set
{
This. _ Name = value;
NotifyPropertyChanged ("Name ");
}
}
Private bool sex;
Public bool Sex
{
Get {return sex ;}
Set
{
Sex = value;
Policypropertychanged ("Sex ");
}
}
Private int _ Age;
Public int Age
{
Get {return this. _ Age ;}
Set
{
This. _ Age = value;
NotifyPropertyChanged ("Age ");
}
}
Public event PropertyChangedEventHandler PropertyChanged;
Public void policypropertychanged (string propertyName)
{
If (PropertyChanged! = Null)
{
PropertyChanged (this, new PropertyChangedEventArgs (propertyName ));
}
}
}
MainWindow. XAML: <Window xmlns: my = "clr-namespace: WpfApplication1" 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">
<DockPanel>
<Grid Height = "300" Width = "300" Background = "# FFDE3030">
<TextBlock Text = "name:" Margin = "42,38, 130,237"/>
<TextBox x: Name = "tbName" Width = "100" Height = "30" Margin = "107,35, 93,235" Text = "{Binding Name, Mode = TwoWay}"/>
<TextBlock Text = "Age:" Margin = "42,69, 199,204" TextTrimming = "None"/>
<TextBox x: Name = "tbAge" Width = "100" Height = "30" Margin = "93,204," Text = "{Binding Age, Mode = TwoWay}"/>
<CheckBox x: Name = "cbxSex" Content = "gender" IsChecked = "{Binding
Sex, Mode = TwoWay} "Height =" 16 "HorizontalAlignment =" Left"
Margin = ", 35," verticalignment = "Top" Width = "62"/>
</Grid> </DockPanel>
</Window>
/// <Summary>
/// MainWindow. xaml background code /// </summary>
Public partial class MainWindow: Window
{
User Myuser = new User ();
Public MainWindow ()
{
InitializeComponent ();
Myuser = new User ();
Myuser. Name = "Liu Lijie ";
Myuser. Age = 23;
TbName. DataContext = Myuser;
TbAge. DataContext = Myuser;
CbxSex. DataContext = Myuser;
Myuser. PropertyChanged + = new System. ComponentModel. PropertyChangedEventHandler (Myuser_PropertyChanged );
}
Void Myuser_PropertyChanged (object sender, System. ComponentModel. PropertyChangedEventArgs e)
{
MessageBox. Show (e. PropertyName. ToString () + "change ");
}