How to use C # INotifyPropertyChanged

Source: Internet
Author: User

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:

  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Text;
  5. Using System.ComponentModel;
  6. Namespace Wpfapp
  7. {
  8. public class person:inotifypropertychanged
  9. {
  10. private String _name = "Zhang San";
  11. private int _age = 24;
  12. private String _hobby = "basketball";
  13. Public String Name
  14. {
  15. Set
  16. {
  17. _name = value;
  18. if (propertychanged! = null)//has changed
  19. {
  20. PropertyChanged (This, new PropertyChangedEventArgs ("Name")); Listen to name
  21. }
  22. }
  23. Get
  24. {
  25. return _name;
  26. }
  27. }
  28. public int Age
  29. {
  30. Set
  31. {
  32. _age = value;
  33. if (propertychanged! = null)
  34. {
  35. PropertyChanged (This, new PropertyChangedEventArgs ("age")); Listening to Age
  36. }
  37. }
  38. Get
  39. {
  40. return _age;
  41. }
  42. }
  43. Public String Hobby//Hobby not listening
  44. {
  45. get { return _hobby;}
  46. set {_hobby = value;}
  47. }
  48. public event PropertyChangedEventHandler propertychanged;
  49. }
  50. }

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:

  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= "width=">
  5. <grid name="Grid" >
  6. <textbox height="text="{Binding path=name} "horizontalalignment=" left"margin=" 63,12,0,0 " Name= "textBox1" verticalalignment="Top" width="139"/>
  7. <textbox height="text="{Binding path=age} "horizontalalignment=" left"margin=" 63,48,0,0 " Name= "textBox2" verticalalignment="Top" width="139"/>
  8. <textbox height="text="{Binding path=hobby} "horizontalalignment=" left"margin=" 63,82,0,0 " Name= "textBox3" verticalalignment="Top" width="139"/>
  9.         <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;
  10. <button content="Modify user Information" height="" Horizontalalignment="left" margin= "60,158,0,0" Name=" Button2 "verticalalignment=" Top "width=" 144 " click=" button2_click "/>
  11. <textblock height="horizontalalignment=" "left" margin="13,201,0,0" name="TextBlock1" text= "{Binding path=name}" verticalalignment="Top" width=" " "/>
  12. <textblock height="horizontalalignment=" "left" margin="118,201,0,0" name="TextBlock2" text= "{Binding path=age}" verticalalignment="Top" width=" " "/>
  13. <textblock height="horizontalalignment=" "left" margin="222,201,0,0" name="TextBlock3" text= "{Binding path=hobby, mode=twoway}" verticalalignment="Top" width="/> "
  14. </Grid>
  15. </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. Namespace Wpfapp
  15. {
  16. // <summary>
  17. /// MainWindow.xaml's interactive logic
  18. // </summary>
  19. Public partial class Mainwindow:window
  20. {
  21. Public MainWindow ()
  22. {
  23. InitializeComponent ();
  24. }
  25. private person P1 = new person ();
  26. private void button1_click (object sender, RoutedEventArgs e)
  27. {
  28. Grid. DataContext = p1; //Bind data
  29. P1.   Name = "John Doe";
  30. P1.  Hobby = "Football";
    1. }
    2. Private void button2_click (object sender, RoutedEventArgs e)
    3. {
    4. P1. Age = P1. Age + 1;
    5. P1.  Hobby = "Football";
    6. }
    7. }
    8. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.