Chuan Zhi podcast -- Data Binding -- INotifyPropertyChanged (), Chuan Zhi podcast
INotifyPropertyChanged is generally used for data binding.
InotifyPropertyChanged is a built-in. net interface. When binding data, it checks whether DataContext has implemented InotifyPropertyChanged. If so, it listens to PropertyChanged and learns that the property has changed.
The InotifyPropertyChanged interface is used to send a notification that a property value has been changed to the client.
Class
class Person:INotifyPropertyChanged { private int age; public int Age { get { return age; } set { this.age = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Age")); } } } public event PropertyChangedEventHandler PropertyChanged; }
. Cs File
public partial class MainWindow : Window { private Person p = new Person(); public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { p.Age++; } private void Window_Loaded(object sender, RoutedEventArgs e) { p.Age = 10; txtAge.DataContext = p; } }
Xaml files
<TextBox Name="txtAge" HorizontalAlignment="Left" Height="23" Margin="82,39,0,0" TextWrapping="Wrap" Text="{Binding Age}" VerticalAlignment="Top" Width="120"/><Button Content="Age++" HorizontalAlignment="Left" Margin="271,39,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
In this way, you can click the button to realize the auto-increment of numbers in the textbox.