WPFS Data Binding (if the property value of the background class object changes, the notification is changed in the "Client interface bound with the control value" requires implementation of the Inotitypropertychanged interface)
MainWindow.xaml
<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=" "width=" 525 "Loade" d= "window_loaded" > <Grid> <textbox text= "{Binding Name}" height= "All" horizontalalignment= "left" M Argin= "122,68,0,0" name= "txtname" verticalalignment= "Top" width= "+"/> <textbox text= "{Binding age}" Heigh T= "horizontalalignment=" left "margin=" 122,124,0,0 "name=" Txtage "verticalalignment=" Top "width=" "/> < ; TextBlock height= "horizontalalignment=" "left" margin= "82,71,0,0" name= "TextBlock1" text= "name" verticalalignment= " Top "/> <textblock height=" "horizontalalignment=" left "margin=" 82,127,0,0 "name=" TextBlock2 "text=" Age "Ve rticalalignment= "Top"/> <button content= "age++" height= "All" horizontalalignment= "left" margin= "262,71,0,0" Name= "Button1" VerticalAlignment= "Top" width= "click=" Button1_Click "/> <button content=" show age "height=" Max "horizontalalignment=" left "Ma Rgin= "262,124,0,0" name= "Button2" verticalalignment= "Top" width= "page" click= "button2_click"/> </Grid> </w
Indow>
MainWindow.xaml.cs
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 WpfApplication1 {//<summary>///MainWindow.xaml interactive logic///</summary> public part
Ial class Mainwindow:window {person P1 = new person ();
Public MainWindow () {InitializeComponent (); The private void window_loaded (object sender, RoutedEventArgs e) {p1.
Name = "Li Dazhao"; P1.
Age = 28;
Txtname.datacontext = p1;
Txtage.datacontext = p1; The private void Button1_Click (object sender, RoutedEventArgs e) {p1.
age++; } private void Button2_Click (ObjeCT sender, RoutedEventArgs e) {MessageBox.Show (P1. Age).
ToString ());
}
}
}
Model
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.ComponentModel; namespace WpfApplication1 {//<summary>///INotifyPropertyChanged interface is a notification to the client that a property value has changed///Inotifypro Pertychanged is the. NET built-in interface, data binding DataContext whether the Inotitypropertychanged interface is implemented, if implemented, will listen to propertychanged know the changes in the properties// If the value of the background object is requested to be changed and the value of the interface changes, then the Inotitypropertychanged interface needs to be implemented.
and triggers the event after the object property value changes///If the value of the background object is not changed, then there is no need to implement this interface//</summary> public class person:inotifypropertychanged
{private string name;
public string Name {get {return name;
} set {this.name = value; if (propertychanged! = null) {//If the Name property has changed, this event is triggered propertychanged (this, new Prope
Rtychangedeventargs ("Name"));
}}} private int age;
public int Age { get {return age;
} set {this.age = value;
If someone (the data-bound object is listening) listens for this event (if someone is listening is not equal to NULL, if no one listens to this event is equal to null) if (propertychanged! = null) {//If the age attribute has changed, this event is triggered propertychanged (this,new PropertyChangedEventArgs
);
}}} public event PropertyChangedEventHandler propertychanged;
}
}