INotifyPropertyChanged usage and data binding in WPF

Keywords: Windows

The INotifyPropertyChanged interface is often used to implement data binding in WPF. Here is a case of INotifyPropertyChanged.

Define a Person class as follows:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ComponentModel;  
  6.   
  7. namespace WpfApp  
  8. {  
  9.     public class Person:INotifyPropertyChanged  
  10.     {  
  11.         private String _name = "Zhang San";  
  12.         private int _age = 24;  
  13.         private String _hobby = "Basketball";  
  14.             
  15.         public String Name  
  16.         {  
  17.             set  
  18.             {  
  19.                 _name = value;  
  20.                 if (PropertyChanged != null)//Changed  
  21.                 {  
  22.                     PropertyChanged(thisnew PropertyChangedEventArgs("Name"));//Monitor Name  
  23.                 }  
  24.             }  
  25.             get  
  26.             {  
  27.                 return _name;  
  28.             }  
  29.         }  
  30.   
  31.         public int Age  
  32.         {  
  33.             set  
  34.             {  
  35.                 _age = value;  
  36.                 if (PropertyChanged != null)  
  37.                 {  
  38.                     PropertyChanged(thisnew PropertyChangedEventArgs("Age"));//Monitor Age  
  39.                 }  
  40.             }  
  41.             get  
  42.             {  
  43.                 return _age;  
  44.             }   
  45.         }  
  46.         public String Hobby//No monitoring of Hobby  
  47.         {  
  48.             get { return _hobby; }  
  49.             set { _hobby = value; }  
  50.         }  
  51.         public event PropertyChangedEventHandler PropertyChanged;  
  52.     }  
  53. }  
In the Person class defined above, we listened to the Name and Age attributes, but not to the bby.

The contents defined in MainWindow.xmal interface file are as follows:

  1. <Window x:Class="WpfApp.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MainWindow" Height="300" Width="350">  
  5.     <Grid Name="grid">   
  6.         <TextBox Height="20" Text="{Binding Path=Name}"  HorizontalAlignment="Left" Margin="63,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="139" />  
  7.         <TextBox Height="20"  Text="{Binding Path=Age}"  HorizontalAlignment="Left" Margin="63,48,0,0" Name="textBox2" VerticalAlignment="Top" Width="139" />  
  8.         <TextBox Height="20" Text="{Binding Path=Hobby}"  HorizontalAlignment="Left" Margin="63,82,0,0" Name="textBox3" VerticalAlignment="Top" Width="139" />  
  9.           
  10.         <Button Content="Display user information" Height="26" HorizontalAlignment="Left" Margin="60,118,0,0" Name="button1" VerticalAlignment="Top" Width="144" Click="button1_Click" />  
  11.         <Button Content="Modify user information" Height="26" HorizontalAlignment="Left" Margin="60,158,0,0" Name="button2" VerticalAlignment="Top" Width="144" Click="button2_Click" />  
  12.   
  13.         <TextBlock Height="40" HorizontalAlignment="Left" Margin="13,201,0,0" Name="textBlock1"   Text="{Binding Path=Name}"  VerticalAlignment="Top" Width="88" />  
  14.         <TextBlock Height="40" HorizontalAlignment="Left" Margin="118,201,0,0" Name="textBlock2" Text="{Binding Path=Age}" VerticalAlignment="Top" Width="88" />  
  15.         <TextBlock Height="40" HorizontalAlignment="Left" Margin="222,201,0,0" Name="textBlock3" Text="{Binding Path=Hobby, Mode=TwoWay}" VerticalAlignment="Top" Width="88" />  
  16.     </Grid>  
  17. </Window>  

The background code is:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Navigation;  
  13. using System.Windows.Shapes;  
  14.   
  15. namespace WpfApp  
  16. {  
  17.     /// <summary>  
  18.     ///Interaction logic of MainWindow.xaml  
  19.     /// </summary>  
  20.     public partial class MainWindow : Window  
  21.     {  
  22.         public MainWindow()  
  23.         {  
  24.             InitializeComponent();  
  25.         }  
  26.   
  27.         private Person p1 = new Person();  
  28.         private void button1_Click(object sender, RoutedEventArgs e)  
  29.         {  
  30.             grid.DataContext = p1;//Binding data  
  31.             p1.Name = "Li Si";   
  1.             p1.Hobby = "Football";  
  2.         }   
  3.         private void button2_Click(object sender, RoutedEventArgs e)  
  4.         {     
  5.             p1.Age = p1.Age + 1;  
  6.             p1.Hobby = "Football";  
  7.         }  
  8.     }  
  9. }  

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.

Posted by keenlearner on Wed, 01 Apr 2020 14:40:36 -0700