C# Snippets for Properties that support INotifyPropertyChanged

Since WPF likes INotifyPropertyChanged on bound objects, I looked for a snippet to help out.

Matthias Shapiro had a good version for the interface implementation and a property declaration. I made a couple adjustments to the snippets based on personal preference (check if the value has changed before raising the event / remove the region from the property.

Then added some property verification support based on the snippet sample from Philipp Sumi. The property verification uses reflection to check if the property exists. If not, it throws an exception. This function is marked with a Conditional attribute so that it is only called in DEBUG builds.

The resulting snippets are as follows:

INotifyPropertyChanged Interface Implementation

<?xml version="1.0" encoding="utf-8" ?>
<
CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<
CodeSnippet Format="1.0.0">
<
Header>
<
Author>
Matthias Shapiro (Original),
Philipp Sumi (VerifyProperty method),
Nathan Allen-Wagner (Modifications)
</Author>
<
HelpUrl>http://blog.alner.net/</HelpUrl>
<
SnippetTypes>
<
SnippetType>Expansion</SnippetType>
</
SnippetTypes>
<
Title>
NotifyObject:
Use this to add the INotifyPropertyChange implementation
to a class. Supports property name verification in DEBUG
builds.
</Title>
<
Shortcut>notifyo</Shortcut>
</
Header>
<
Snippet>
<
Code Language="csharp">
<![CDATA[
#region INotifyPropertyChanged

/// <summary>
/// The PropertyChanged event is used by consuming code
/// (like WPF's binding infrastructure) to detect when
/// a value has changed.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;

/// <summary>
/// Raise the PropertyChanged event for the
/// specified property.
/// </summary>
/// <param name="propertyName">
/// A string representing the name of
/// the property that changed.</param>
/// <remarks>
/// Only raise the event if the value of the property
/// has changed from its previous value</remarks>
protected void OnPropertyChanged(string propertyName)
{
// Validate the property name in debug builds
VerifyProperty(propertyName);

if(null != PropertyChanged)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

/// <summary>
/// Verifies whether the current class provides a property with a given
/// name. This method is only invoked in debug builds, and results in
/// a runtime exception if the <see cref="OnPropertyChanged"/> method
/// is being invoked with an invalid property name. This may happen if
/// a property's name was changed but not the parameter of the property's
/// invocation of <see cref="OnPropertyChanged"/>.
/// </summary>
/// <param name="propertyName">The name of the changed property.</param>
[System.Diagnostics.Conditional("DEBUG")]
private void VerifyProperty(string propertyName)
{
Type type = this.GetType();

// Look for a *public* property with the specified name
System.Reflection.PropertyInfo pi = type.GetProperty(propertyName);
if (pi == null)
{
// There is no matching property - notify the developer
string msg = "OnPropertyChanged was invoked with invalid " +
"property name {0}. {0} is not a public " +
"property of {1}.";
msg = String.Format(msg, propertyName, type.FullName);
System.Diagnostics.Debug.Fail(msg);
}
}

#endregion
$end$
]]>
</
Code>
</
Snippet>
</
CodeSnippet>
</
CodeSnippets>

Property Declaration for INotifyPropertyChanged

<?xml version="1.0" encoding="utf-8" ?>
<
CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<
CodeSnippet Format="1.0.0">
<
Header>
<
Author>
Matthias Shapiro (Original),
Nathan Allen-Wagner (Modifications)
</Author>
<
HelpUrl>http://blog.alner.net/</HelpUrl>
<
SnippetTypes>
<
SnippetType>Expansion</SnippetType>
</
SnippetTypes>
<
Title>
INotifyPropertyChanged Property: Use this to create a
new property INotifyPropertyChanged implementation.
</Title>
<
Shortcut>notifyp</Shortcut>
</
Header>
<
Snippet>
<
Declarations>
<
Literal>
<
ID>publicProperty</ID>
<
ToolTip>The name of the private property</ToolTip>
<
Default>MyProperty</Default>
</
Literal>
<
Literal>
<
ID>type</ID>
<
ToolTip>
The type of the property
(e.g. string, double, bool, Brush, etc.)
</ToolTip>
<
Default>string</Default>
</
Literal>
</
Declarations>
<
Code Language="csharp">
<![CDATA[
private $type$ _$publicProperty$;
public $type$ $publicProperty$
{
get{ return _$publicProperty$;}
set
{
if (value == _$publicProperty$)
return;

_$publicProperty$ = value;
OnPropertyChanged("$publicProperty$");
}
}

$end$
]]>
</
Code>
</
Snippet>
</
CodeSnippet>
</
CodeSnippets>

 

As usual, use at your own risk. Let me know if you find any issues.

Cheers!

One thought on “C# Snippets for Properties that support INotifyPropertyChanged

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s