INotifyPropertyChanged interface: Notifies the client that a property value has changed.the Notifypropertychanged interface is used to send a notification that a property value has changed for the client (typically the client performing the binding).
The usual place to use is: when loading data, update the corresponding data load name in time. When operating the function, prompt the corresponding error message in time.
Instance:
XAML Code:
<textblock margin= "80,5,80,0" textwrapping= "Wrap" foreground= "White" fontfamily= "Microsoft Jas Black" name= "Txtinfo" Text= "{ Binding message, Mode=twoway} "tooltip=" {Binding message} "texttrimming=" WordEllipsis "grid.row=" 2 "fontsize=" > </TextBlock>
Background code:
private string _message = String. Empty;
<summary>
Error message
</summary>
public string Message
{
get {return _message;}
Set
{
_message = value;
Use a message in order to react to the control, directly to the _message assignment cannot be directly reflected in the control
Notifypropertychanged ("Message");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void notifypropertychanged (String propertyname)
{
if (this. PropertyChanged = null)
{
This. PropertyChanged (This, new PropertyChangedEventArgs (PropertyName));
}
}
Message Assignment Value:
Message = "Loading data!";
Detailed examples (plagiarism):
The INotifyPropertyChanged interface is often used to implement data binding in WPF, and a inotifypropertychanged case is shown below.
The following defines a person class:
- 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)//has changed
- {
- PropertyChanged (This, new PropertyChangedEventArgs ("Name")); Listen to name
- }
- }
- Get
- {
- return _name;
- }
- }
- public int Age
- {
- Set
- {
- _age = value;
- if (propertychanged! = null)
- {
- PropertyChanged (This, new PropertyChangedEventArgs ("age")); Listening to Age
- }
- }
- Get
- {
- return _age;
- }
- }
- Public String Hobby//Hobby not listening
- {
- get { return _hobby;}
- set {_hobby = value;}
- }
- public event PropertyChangedEventHandler propertychanged;
- }
- }
In the person class defined above, the name and age properties are monitored, but the hobby is not monitored.
The contents of the Mainwindow.xmal interface file are defined 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= "width=">
- <grid name="Grid" >
- <textbox height="text="{Binding path=name} "horizontalalignment=" left"margin=" 63,12,0,0 " Name= "textBox1" verticalalignment="Top" width="139"/>
- <textbox height="text="{Binding path=age} "horizontalalignment=" left"margin=" 63,48,0,0 " Name= "textBox2" verticalalignment="Top" width="139"/>
- <textbox height="text="{Binding path=hobby} "horizontalalignment=" left"margin=" 63,82,0,0 " Name= "textBox3" verticalalignment="Top" width="139"/>
- <button content= "Show user Information" height= " horizontalalignment= "left" margin= "60,118,0,0" name= "button1" verticalalignment= "Top" width= "144" click= "button1_ Click "&NBSP;/>&NBSP;&NBSP;
- <button content="Modify user Information" height="" Horizontalalignment="left" margin= "60,158,0,0" Name=" Button2 "verticalalignment=" Top "width=" 144 " click=" button2_click "/>
- <textblock height="horizontalalignment=" "left" margin="13,201,0,0" name="TextBlock1" text= "{Binding path=name}" verticalalignment="Top" width=" " "/>
- <textblock height="horizontalalignment=" "left" margin="118,201,0,0" name="TextBlock2" text= "{Binding path=age}" verticalalignment="Top" width=" " "/>
- <textblock height="horizontalalignment=" "left" margin="222,201,0,0" name="TextBlock3" text= "{Binding path=hobby, mode=twoway}" verticalalignment="Top" width="/> "
- </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>
- /// MainWindow.xaml's interactive logic
- // </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; //Bind data
- P1. Name = "John Doe";
- P1. Hobby = "Football";
- }
- Private void button2_click (object sender, RoutedEventArgs e)
- {
- P1. Age = P1. Age + 1;
- P1. Hobby = "Football";
- }
- }
- }
When you click Show User Data
Let's see where this information comes from.
Because hobby is not being monitored in person, p1. hobby= the phrase "football" does not play a role. When you click Modify user information, it is also not possible to modify the information that is bound to the corresponding hobby on the interface (even if the Mode=twoway is written at the interface, it cannot be bound).
So when you use INotifyPropertyChanged, you need to set the properties that you want to bind to, or you cannot bind them in a two-way bind, that is, the binding is not valid.
How to use C # INotifyPropertyChanged