Skip Navigation LinksHome > View Post
WPF Snippets for Visual Studio

When talking to customers about WPF I often get asked for a copy of the INotifyPropertyChanged snippets I use in Visual Studio. Well, here they are:

UPDATE - New snippets available here!

To add them to Visual Studio 2008 go to Tools | Code Snippets Manager.

Tools | Code Snippets Manager

Click 'Add' and select the 'Snippets' folder unpacked from the zip above.

Add Snippets

You should now have three new snippets:

inpc



public class MyClass : INotifyPropertyChanged
{


public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler pceh = PropertyChanged;
if (pceh != null)
{
pceh(this, new PropertyChangedEventArgs(propertyName));
}
}
}

You'll probably have to add the System.ComponentModel namespace to your usings so INotifyPropertyChanged is in scope. Follow this link for a tip showing the easy way.

propnp



private string myField;

public string MyProperty
{
get { return myField; }
set
{
myField = value;
OnPropertyChanged("MyProperty");
}
}

proproc



private readonly ObservableCollection<string> myField = new ObservableCollection<string>();

public ObservableCollection<string> MyProperty
{
get { return myField; }
}

Hope that helps.

Post a comment:

Name  

E-mail (never shared)

URL

Comments  

Captcha ImageRefresh Image
What's this?
Enter code above