The INotifyPropertyChanged interface is often used to implement data binding in WPF. Here is a case of INotifyPropertyChanged.
Define a Person class as follows:
In the Person class defined above, we listened to the Name and Age attributes, but not to the bby.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ComponentModel;
- namespace WpfApp
- {
- public class Person:INotifyPropertyChanged
- {
- private String _name = "Zhang San";
- private int _age = 24;
- private String _hobby = "Basketball";
- public String Name
- {
- set
- {
- _name = value;
- if (PropertyChanged != null)//Changed
- {
- PropertyChanged(this, new PropertyChangedEventArgs("Name"));//Monitor Name
- }
- }
- get
- {
- return _name;
- }
- }
- public int Age
- {
- set
- {
- _age = value;
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs("Age"));//Monitor Age
- }
- }
- get
- {
- return _age;
- }
- }
- public String Hobby//No monitoring of Hobby
- {
- get { return _hobby; }
- set { _hobby = value; }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- }
- }
The contents defined in MainWindow.xmal interface file are as follows:
- <Window x:Class="WpfApp.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="300" Width="350">
- <Grid Name="grid">
- <TextBox Height="20" Text="{Binding Path=Name}" HorizontalAlignment="Left" Margin="63,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="139" />
- <TextBox Height="20" Text="{Binding Path=Age}" HorizontalAlignment="Left" Margin="63,48,0,0" Name="textBox2" VerticalAlignment="Top" Width="139" />
- <TextBox Height="20" Text="{Binding Path=Hobby}" HorizontalAlignment="Left" Margin="63,82,0,0" Name="textBox3" VerticalAlignment="Top" Width="139" />
- <Button Content="Display user information" Height="26" HorizontalAlignment="Left" Margin="60,118,0,0" Name="button1" VerticalAlignment="Top" Width="144" Click="button1_Click" />
- <Button Content="Modify user information" Height="26" HorizontalAlignment="Left" Margin="60,158,0,0" Name="button2" VerticalAlignment="Top" Width="144" Click="button2_Click" />
- <TextBlock Height="40" HorizontalAlignment="Left" Margin="13,201,0,0" Name="textBlock1" Text="{Binding Path=Name}" VerticalAlignment="Top" Width="88" />
- <TextBlock Height="40" HorizontalAlignment="Left" Margin="118,201,0,0" Name="textBlock2" Text="{Binding Path=Age}" VerticalAlignment="Top" Width="88" />
- <TextBlock Height="40" HorizontalAlignment="Left" Margin="222,201,0,0" Name="textBlock3" Text="{Binding Path=Hobby, Mode=TwoWay}" VerticalAlignment="Top" Width="88" />
- </Grid>
- </Window>
The background code is:
- 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;
- namespace WpfApp
- {
- /// <summary>
- ///Interaction logic of MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- private Person p1 = new Person();
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- grid.DataContext = p1;//Binding data
- p1.Name = "Li Si";
- p1.Hobby = "Football";
- }
- private void button2_Click(object sender, RoutedEventArgs e)
- {
- p1.Age = p1.Age + 1;
- p1.Hobby = "Football";
- }
- }
- }
When clicking to display user data
Let's see where the information comes from?
Because there is no monitoring of hobby in Person, p1.Hobby = "football" does not work. When you click modify user information, you cannot modify the corresponding hobby information bound to the interface (even if Mode=TwoWay is written in the interface, you cannot bind).
So when using INotifyPropertyChanged, you need to set the display of the properties to be bound. Otherwise, the two-way binding cannot be performed, that is, the binding is invalid.