DCSIMG
propnot – Code Snippet for Property with PropertyChanged event - Guy Burstein's Blog

Guy Burstein's Blog

Developer Evangelist @ Microsoft

News

Guy Burstein The Bu

Disclaimer
Postings are provided 'As Is' with no warranties and confer no rights.

Guy Burstein LinkedIn Profile

propnot – Code Snippet for Property with PropertyChanged event

propnot – Code Snippet for Property with PropertyChanged event

propnot - Code Snippet for PropertyChanged event

Using Silverlight 2 data binding, If we want the UI to be notified and updated with changes in the model, we have to implement System.ComponentModel.INotifyPropertyChanged interface:

public interface INotifyPropertyChanged

{

    event PropertyChangedEventHandler PropertyChanged;

}

A property that wants to notify the program that its values was changed should raise the PropertyChanged event and provide the name of the property. For example:

public class Person : INotifyPropertyChanged

{

    private string name;

    public string Name

    {

        get

        {

            return this.name;

        }

        set

        {

            this.name = value;

            if (this.PropertyChanged != null)

                this.PropertyChanged(this, new PropertyChangedEventArgs(Name));

        }

    }

 

    #region INotifyPropertyChanged Members

 

    public event PropertyChangedEventHandler PropertyChanged;

 

    #endregion

}

In case you like to code fast as I like, and you use code snippets to speed things up, you can use propnot – code snippet with PropertyChanged event implementation like the sample above.

To use the snippet:

  1. Download this zip file.
  2. Extract the propnot.snippet file to your custom code snippets directory (C:\Users\guyb\Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets).
  3. Use it in Visual Studio!

Enjoy!

Comments

No Comments