INotifyPropertyChanged was introduced in .NET 2.0. This interface has only one member, the PropertyChanged event that will notify listeners when a property of the implementing class is changed.

Demo

  • Open Visual Studio and select WPF application.
  • Open/add one user control or window.
  • Create one normal property, here I have added FirstName as in the following:
    1. private string firstname = string.Empty;
    2. public string FristName
    3. {
    4. get { return firstname; }
    5. set
    6. {
    7. firstname = value;
    8. }
    9. }
  • Add the same property into two controls as in the following:
    1. <TextBlock Margin="2">First Name</TextBlock>
    2. <TextBox Margin="2" Grid.Row="0" Grid.Column="1" MinWidth="120" Text="{Binding Path=FristName,ElementName=ThisControl,UpdateSourceTrigger=PropertyChanged}"></TextBox>
    3. <TextBlock Margin="2" Grid.Column="2" Text="{Binding Path=FristName,ElementName=ThisControl,UpdateSourceTrigger=PropertyChanged}"></TextBlock>
  • Run the application, I entered the first name field but not displaying in my TextBlock in the right side.





  • But we can see the property change in the preceding picture. But it's not reflecting it in the UI.
  • Implement the INotifyPropertyChanged interface, no change in the XAML.
    1. public partial class INotify : UserControl, INotifyPropertyChanged
    2. public INotify()
    3. {
    4. InitializeComponent();
    5. }
    6. private string firstname = string.Empty;
    7. public string FristName
    8. {
    9. get { return firstname; }
    10. set
    11. {
    12. firstname = value;
    13. OnPropertyChanged("FristName");
    14. }
    15. }
    16. #region INotifyPropertyChanged Members
    17. public event PropertyChangedEventHandler PropertyChanged;
    18. public void OnPropertyChanged(string txt)
    19. {
    20. PropertyChangedEventHandler handle = PropertyChanged;
    21. if (handle != null)
    22. {
    23. handle(this, new PropertyChangedEventArgs(txt));
    24. }
    25. }
    26. #endregion
  • Run the application and see the change.


  • We got the change in the UI.

Conclusion

The INotifyPropertyChanged interface will notify the source of changes to the target.