Archive

Posts Tagged ‘INotifyPropertyChanged’

Code Snippet – proprpc

May 26, 2011 1 comment

Anyone who has played around with WPF or Silverlight for while will have come across the INotifyPropertyChanged interface and the RaisePropertyChanged method.

In an earlier post I highlighted one possible solution, though the use of an extension method, for calling the RaisePropertyChanged method without having to use “magic strings”.

I now use this extensively in my day to day WPF work, however I still sometimes forget to include the RaisePropertyChanged call in the property setter which then takes some time to debug.

In order to avoid this I’ve finally created a code snippet that will insert a field backed property that also calls RaisePropertyChanged.

The snippet is called ‘proprpc‘ so that it is line with the other property snippets of ‘prop‘, ‘propdp‘, ‘propfull‘, etc.

Download links are given below for the .snippet file (including a zipped version) and a Visual Studio Installer file.

WPF, MVVM and RaisePropertyChanged

July 21, 2010 2 comments

Several times recently during development I have come across situations where specific strings are required for functionality to work effectively and yet not having the correct string will not cause any compilation or run time errors, features just won’t work. The features may be subtle enough such that it could be some time before any break in functionality is noticed and therefore could reach production code. In coding terms these are called “magic strings” and are something to be avoided at all costs.

One example of this I have come across often in MVVM WPF or Silverlight projects where the ViewModels implement the INotifyPropertyChanged interface such that when a property is changed it is common to see code such as the following.

private string _firstName;
public string FirstName
{
    get { return _firstName; }
    set
    {
        if (_firstName != value)
        {
            _firstName = value;
            RaisePropertyChanged("FirstName");
        }
    }
}

The alarm bell for me here is the hard coded name of the property “FirstName” within the RaisePropertyChanged method. In this simple example it is clear, but in more complex code it can easily get lost and also magic strings such as these won’t be picked up and adjusted in any automated refactoring of the names of the properties.

You can obtain the name of the property using the following bit of code, however it includes either ‘get_’ or ‘set_’ at the start depending upon where it is called.

MethodBase.GetCurrentMethod().Name;

Thus I’ve created the following extension method to take the property name and simply strim off the first 4 characters thus leaving you with the correct property name.

public static string GetPropertyName(this MethodBase methodBase)
{
    return methodBase.Name.Substring(4);
}

Using this extension method, as below, it means that you will be able to raise property changed events without concern that in future your property name might change.

RaisePropertyChanged(MethodBase.GetCurrentMethod().GetPropertyName());

Update: I’ve now created a Visual Studio code snippet that is used for creating field backed properties that automatically includes the “RaisePropertyChanged” call which can be found here.