Detailed description of the INotifyPropertyChanged interface

Detailed description of the INotifyPropertyChanged interface

In Windows Phone Development 8.1: Data Binding, we learned the basics of data binding. In the next few articles, we will continue to learn more about data binding. Today, we will look at the implementation of the INotifyPropertyChanged interface, which is very important in data binding.
When to implement the INotifyPropertyChanged interface

Official explanation: The INotifyPropertyChanged interface is used to notify the client (usually the client that performs the binding) that a property value has changed. The official explanation is very vague, and I guess I don’t know when I need to implement the INotifyPropertyChanged interface. Xiao Meng gave a clear conclusion through actual testing:

First: OneTime mode: It is meaningless, because its binding is only once at the beginning, and there is no change at all! Naturally, there is no implementation of the INotifyPropertyChanged interface.

Then there is the OneWay mode: We know that the meaning of the OneWay mode is that every change in the binding source will notify the binding target, but the change of the binding target will not change the binding source. When the data entity class of the binding source does not implement the INotifyPropertyChanged interface, when we change the data source, we will find that the corresponding data on the UI of the binding target will not change immediately. So at this time we need to implement the INotifyPropertyChanged interface.

*** is the TwoWay mode: In the TwoWay mode, when the data entity class of the binding source does not implement the INotifyPropertyChanged interface, we find that the change of the control will cause the data source to change immediately, but the change of the data source will not change the binding target control immediately! So when we need the corresponding UI to change immediately when the data source changes, we need to implement the INotifyPropertyChanged interface.

In short: when the data source changes and the UI needs to change immediately, we need to implement the INotifyPropertyChanged interface.

We can clearly see this through this example:

  1. <StackPanel>
  2.  
  3. <TextBox Header= "Number" Text= "{Binding ID,Mode=OneTime}" Name= "tbxID" ></TextBox>
  4.  
  5. <TextBox Header= "Book Title" Text= "{Binding Title,Mode=OneWay}" Name= "tbxTitle" ></TextBox>
  6.  
  7. <TextBox Header= "Price" Text= "{Binding Price,Mode=TwoWay}" Name= "tbxPrice" ></TextBox>
  8.  
  9. <Button Content= "Modify the value of the control through the data source" Click= "Button_Click" ></Button>
  10.  
  11. <Button Content= "Modify the value of the control directly" Click= "Button_Click_1" />
  12.  
  13. <Button Content= "Modify the value of the data source through the control" Click= "Button_Click_2" />
  14.  
  15. </StackPanel>

Background code

  1. namespace INotifyPropertyChangedDEMO
  2. {
  3. /// <summary>  
  4. /// Can be used by itself or to navigate to a blank page inside a Frame.  
  5. /// </summary>  
  6.  
  7. public sealed partial class MainPage : Page
  8. {
  9. Book book = new Book();
  10. public MainPage()
  11. {
  12. this .InitializeComponent();
  13.  
  14. this .NavigationCacheMode = NavigationCacheMode.Required;
  15. book.ID = 0 ;
  16. book.Title = "ASP.NET Development Manual" ;
  17. book.Price = 40 ;
  18. st.DataContext = book;
  19. }
  20. private   void Button_Click(object sender, RoutedEventArgs e) //Modify the value of the control by modifying the data source  
  21. {
  22. book.ID = 100 ;
  23. book.Price = 50 ;
  24. book.Title = "SL Development Manual" ;
  25. }
  26.  
  27. private async void Button_Click_1(object sender, RoutedEventArgs e) //Display the value of the data source  
  28. {
  29. await new MessageDialog(book.ID.ToString() + " " + book.Title.ToString() + " " + book.Price.ToString()).ShowAsync();
  30. }
  31.  
  32. public   class Book: INotifyPropertyChanged
  33. //The INotifyPropertChanged interface defines an event that is executed when a property value changes. The event name is PropertyChanged.  
  34. //This is an event that must be implemented by the class that inherits this interface  
  35.  
  36. {
  37. private   int _id;
  38. public   int ID
  39. {
  40. get { return _id; }
  41. set
  42. {
  43. _id = value;
  44. //NotifyPropertyChange("ID");  
  45. }
  46. }
  47. private string _title;
  48. public string Title
  49. {
  50. get { return _title; }
  51. set
  52. {
  53. _title = value;
  54. //NotifyPropertyChange("Title");  
  55. }
  56. }
  57. private   double _price;
  58. public   double price
  59. {
  60. get { return _price; }
  61. set
  62. {
  63. _price = value;
  64. //NotifyPropertyChange("Price");  
  65. }
  66. }
  67. public event PropertyChangedEventHandler PropertyChanged;
  68. //PropertyChangedEventArgs type, this class is used to pass the name of the property whose value has changed, and to send a change notification to the client for the property that has changed. The name of the property is a string type.  
  69. private   void NotifyPropertyChange(string propertyName)
  70. {
  71. if (PropertyChanged != null )
  72. {
  73. //According to the delegate class of the PropertyChanged event, implement the PropertyChanged event:  
  74. PropertyChanged( this , new PropertyChangedEventArgs(propertyName));
  75. }
  76. }
  77. }
  78. }
  79. }

You can clearly experience the role of the INotifyPropertyChanged interface by running this example.
How to implement INotifyPropertyChanged interface

The implementation of the INotifyPropertyChanged interface in the above example is the most common and universal one.

We can simplify this by using the CallerMemberNameAttribute attribute, which determines which attribute name to pass in based on the caller:

  1. protected   void OnPropertyChanged([CallerMemberName] string propertyName = null )
  2. {
  3. var eventHandler = this .PropertyChanged;
  4. if (eventHandler != null )
  5. eventHandler( this , new PropertyChangedEventArgs(propertyName));
  6. }

So we can call it like this:

NotifyPropertyChange("ID") is changed to:OnPropertyChanged();

The best implementation of the INotifyPropertyChanged interface:

This so-called *** implementation method is mentioned in the video of channel 9. The implementation method is as follows:

  1. public   class ModelBase : INotifyPropertyChanged
  2. {
  3. public event PropertyChangedEventHandler PropertyChanged;
  4. protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null )
  5. {
  6. if (object.Equals(storage, value)) return   false ;
  7. storage = value;
  8. this .OnPropertyChanged(propertyName);
  9. return   true ;
  10. }
  11.  
  12. protected   void OnPropertyChanged([CallerMemberName] string propertyName = null )
  13. {
  14. var eventHandler = this .PropertyChanged;
  15. if (eventHandler != null )
  16. eventHandler( this , new PropertyChangedEventArgs(propertyName));
  17. }
  18. }

The corresponding calling method is further simplified:

  1. private string name;
  2.  
  3. public string Name
  4. {
  5. get { return name; }
  6. set
  7. { this .SetProperty(ref this .name, value); }
  8. }

Link to this article: http://www.cnblogs.com/bcmeng/p/3966931.html

<<:  APICloud, China's first "cloud-in-one" mobile application cloud released

>>:  Android interview, how to interact with Service

Recommend

The fission gameplay of Weibo traffic diversion!

I have been in the Internet circle for 6 years an...

Sina Micro Wealth's Rapid March: A Subversive Game for Latecomers

It seems that overnight, those who used to check ...

What should I do if I experience roller coaster-like dizziness?

Author: Xiao Lin China Rehabilitation Research Ce...

Online Promotion: How to plan a successful event?

How is a complete event planned and implemented? ...

Meituan Operations: The Marketing Logic of Meituan Takeaway Monthly Card

Buy monthly pass at super low price First, let’s ...

Amazon Athena is now available in AWS China (Ningxia) Region

The new pay-as-you-go interactive query service c...

Why do social traffic platforms flock to offline festivals?

Social traffic platforms refer to platforms that ...

Moji "Air Fruit" is in an awkward situation

Since it is an Internet company that wants to make...

Android Framework Problem Analysis Case - Who Killed the Desktop?

The opportunity to write this article is due to a...

How to make users fall in love with watching ads?

No one likes to watch ads, but everyone needs to ...