Click here to Skip to main content
Click here to Skip to main content

Implementing INotifyPropertyChanged

By , 17 Mar 2010
 

Introduction

If an object implements the INotifyPropertyChanged interface, it’ll raise a property changed event when its property changes. Let's create a sample application to know how we can implement the INotifyPropertyChanged Interface. I am creating a Silverlight application which demonstrates data binding with both an object implementing INotifyPropertyChanged and also a normal DependencyProperty.

  • Open Visual Studio and select a new Silverlight application.
  • Create a class named Customer in the Silverlight project and implement INotifyPropertyChanged.
    public class Customer : INotifyPropertyChanged
    {
  • Define the INotifyPropertyChanged members:
    public event PropertyChangedEventHandler PropertyChanged;
    
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
       if (PropertyChanged != null)
       {
          PropertyChanged(this, e);
       }
    }
  • In the property setter, invoke OnPropertyChanged by passing a property name like:
    private string _Name;
    
    public string Name
    {
      get
      {
         return _Name;
      }
      set
      {
         _Name = value;
         OnPropertyChanged(new PropertyChangedEventArgs("Name"));
      }
    }
  • In MainPage.xaml.cs, add an ObservableCollection of Customer objects as a Dependency Property in order to make sure that the UI is updating while we assigning the customer list to another list or object. If we are making it as a normal property, the UI will update only if we add a new object to the customer list or if any change occurs to the underlying properties.
    public ObservableCollection<Customer> CustomerList
    {
      get { return (ObservableCollection<Customer>)
           GetValue(CustomerListProperty); }
      set { SetValue(CustomerListProperty, value); }
    }
    
    // Using a DependencyProperty as the backing store for MyProperty.
    This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CustomerListProperty =
    DependencyProperty.Register("CustomerList",
    typeof(ObservableCollection<Customer>), typeof(MainPage),
        new PropertyMetadata(new ObservableCollection<Customer>()));
  • I also added a DependencyProperty, FirstName, in MainPage.xaml.cs just to show the binding of a simple DependencyProperty.
    public string FirstName
    {
      get { return (string)GetValue(FirstNameProperty); }
      set { SetValue(FirstNameProperty, value); }
    }
    
    // Using a DependencyProperty as the backing store for MyProperty.
    This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FirstNameProperty =
      DependencyProperty.Register("FirstName", typeof(string), typeof(MainPage),
      new PropertyMetadata(string.Empty)); 
  • In MainPage.XAML, add a DataGrid and TextBox and bind them to the ObservableCollection and DependencyProperty, respectively.
    <data:DataGrid AutoGenerateColumns="True"
       Width="400"
       Height="300"
       ItemsSource="{Binding ElementName=TestUC,
                                    Path=CustomerList}">
    </data:DataGrid>
    <TextBox x:Name="NameTextBox"
       Text="{Binding ElementName=TestUC, Path=FirstName, Mode=TwoWay}"
       Width="100"
       Height="25"
       Margin="0,10,0,10" />
  • For understanding the PropertyChanged event, I added a button and just updated the Customer object in the Click event so that you can see the changes in the DataGrid. When you change a property of the Customer object from the Click event, you can see that the UI is updating accordingly.
  • Download Sample application from Implementing INotifyPropertyChanged.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Arun Jacob
Software Developer Tata Consultancy Services
India India
Member
I have been working in different .NET Technologies like ASP.NET,WPF,Silverlight for the last few years.I am enjoying my life as a Programmer and spending time with my Family,Friends & Camera.
 
My Technical Blog


My Photo Blog


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralSuggestionmemberRichard Deeming25 Mar '10 - 7:16 
GeneralGood article; small sidenotememberAndrei Rinea8 Sep '09 - 0:46 
I would stress out the importance of the order of notification and field set. I, once, mixed them by error and got strange behavior.. I even posted it on Stackoverflow.com : http://stackoverflow.com/questions/1361783/silverlight-checkbox-two-way-binding-not-working-as-expected/1361842#1361842
 

GeneralRe: Good article; small sidenotememberArun Jacob8 Sep '09 - 3:09 
GeneralRe: Good article; small sidenotememberAndrei Rinea8 Sep '09 - 5:14 
GeneralRe: Good article; small sidenotememberArun Jacob8 Sep '09 - 18:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 18 Mar 2010
Article Copyright 2009 by Arun Jacob
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid