New snippets for Silverlight and WPF

A while back I posted some snippets for WPF here: WPF Snippets for Visual Studio. I've been wanting to update these for a while now and finally got around to it this morning. Here are the new snippets.

basevm

This is a snippet that effectively replaces the inpc snippet (although that is still included for demos etc). The primary addition is that of a SetValue method that actually checks if values have been changed before raising the PropertyChanged event. Here's what the snippet looks like.

// TODO - consider making this class abstract if it is used as a base class
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

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

/// <summary>
/// Set's the value of the target parameter (replacing the reference) if the value has changed. Also fires
/// a PropertyChanged event if the value has changed.
/// </summary>
/// <typeparam name="T">The type of the property</typeparam>
/// <param name="target">The target to be swapped out, if different to the value parameter</param>
/// <param name="value">The new value</param>
/// <param name="changedProperties">A list of properties whose value may have been impacted by this change and whose PropertyChanged event should be raised</param>
/// <returns>True if the value is changed, False otherwise</returns>
protected virtual bool SetValue<T>(ref T target, T value, params string[] changedProperties)
{
if (Object.Equals(target, value))
{
return false;
}

target = value;

foreach (string property in changedProperties)
{
OnPropertyChanged(property);
}

return true;
}
}

Note that it's also hinting that this would make a good base class for your ViewModel classes - but that's up to you.

propsv

This is the accompanying replacement for basevm - trumping propnp (although again, that is included for inpc users).

private int _age;

public int Age
{
get { return _age; }
set
{
SetValue(ref _age, value, "Age");
}
}

SetValue takes a list of strings specifying what PropertyChanged events should fire. This is because you often want to fire Changes for dependant properties. E.g.

public bool IsLegalDrinkingAgeInUK
{
get { return _age > 18; }
}

private int _age;

public int Age
{
get { return _age; }
set
{
SetValue(ref _age, value, "Age", "IsLegalDrinkingAgeInUK");
}
}

SetValue also returns a bool in case you want to do some conditional processing based on whether the value was actually changed or not, e.g.:

public int Age
{
get { return _age; }
set
{
if (SetValue(ref _age, value, "Age", "IsLegalDrinkingAgeInUK"))
{
DoSomethingWhenAgeChanges();
}
}
}

depprop

Finally, I've really been wanting to add this to my snippets. Basically some enhancements to the built-in propdp with these new features:

  • Adds a Change handler for the dependency property
  • This one favours Silverlight by going for PropertyMetadata, instead of UIPropertyMetadata (WPF users just put the 'UI' back in)
  • Sets the default value of the dependency property using the default(type) syntax - should avoid nasty errors when you leave '0' instead of '0d' for a double,e tc.

Here's how it looks:

public double Minimum
{
get { return (double)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}

public static readonly DependencyProperty MinimumProperty =
DependencyProperty.Register("Minimum", typeof(double), typeof(MyDependencyObject), new PropertyMetadata(default(double), MinimumChanged));

private static void MinimumChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
{
MyDependencyObject instance = (MyDependencyObject)source;
// TODO - implement change handling here
}

Download

Enjoy: Download Snippets.zip (5KB) .

For installation instructions see here: WPF Snippets for Visual Studio.

If you have any other suggestions for snippets you'd like to see, please leave a comment.

Originally posted by Josh Twist on 30 July 2009 here.