Notify binding for static properties in static classes

Similar to an implementation of INotifyPropertyChanged, static property change notification only works if you use the correct property name when firing the StaticPropertyChanged event.

Use the property name, not the name of the backing field:

public static string ErrorMessgae
{
    get { return errorMessage; }
    set
    {
        errorMessage = value;
        NotifyStaticPropertyChanged("ErrorMessgae"); // not "errorMessage"
    }
}

You should certainly also fix the misspelled property name:

public static string ErrorMessage
{
    get { return errorMessage; }
    set
    {
        errorMessage = value;
        NotifyStaticPropertyChanged("ErrorMessage");
    }
}

The binding should look like this:

Text="{Binding Path=(error:InteractionData.ErrorMessage)}"

See this blog post for details about static property change notification.


You may also avoid to write property names at all by using the CallerMemberNameAttribute:

using System.Runtime.CompilerServices;
...

public static event PropertyChangedEventHandler StaticPropertyChanged;

private static void NotifyStaticPropertyChanged(
    [CallerMemberName] string propertyName = null)
{
    StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
}

You could now call the method without explicitly specifying the property name:

NotifyStaticPropertyChanged();

Leave a Comment