Home > 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.

 
Josh Post By Josh Twist
7:39 AM
03 Feb 2009

» Next Post: The lean, mean, rebuild-ready machine
« Previous Post: Showing a Splash Screen whilst initializing a WPF Prism application

Comments are closed for this post.

© 2005 - 2017 Josh Twist - All Rights Reserved.