-2

I found a notify property changed example for static properties in static class. but it doesn't update any changes in TextBlock. Here are the codes.

First binding is working with the "test" string in constructor but StaticPropertyChanged is always null.

public static class InteractionData
{
    public static List<string> SelectedDirectories { get; set; }
    private static string errorMessage { get; set; }
    public static string ErrorMessgae
    {
        get { return errorMessage; }
        set
        {
            errorMessage = value;
            NotifyStaticPropertyChanged("errorMessage");
        }
    }

    static InteractionData()
    {
        SelectedDirectories = new List<string>();
        errorMessage = "test";
    }

    public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
    private static void NotifyStaticPropertyChanged(string propertyName)
    {
        if (StaticPropertyChanged != null)
            StaticPropertyChanged(null, new PropertyChangedEventArgs(propertyName));
    }
}

In View ...

     xmlns:error ="clr-namespace:CopyBackup.Providers"
<TextBlock Text="{Binding Source={x:Static error:InteractionData.ErrorMessgae} ,Mode=OneWay,  UpdateSourceTrigger=PropertyChanged}"/>

Wherever I change the property, TextBlock doesn't update.

Appreciate

Clemens
  • 123,504
  • 12
  • 155
  • 268
Parham.D
  • 1,158
  • 2
  • 12
  • 31

1 Answers1

12

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();
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • I think the class also needs to implement INotifyPropertChanged, so class cant be static. Im not sure if this works though – M.kazem Akhgary Jan 24 '17 at 09:35
  • 2
    @M.kazemAkhgary That is not necessary. Please take a look at the blog post linked in the answer. – Clemens Jan 24 '17 at 09:57
  • @Clemens I corrected the property name, but it still didn't work. Then, when I used the sample codes in the link, it worked. Thanks again. – Parham.D Jan 26 '17 at 14:54
  • 1
    Nevertheless the code in my answer shows a working solution for static property change notification, and hence answers your question (in other words, solves your problem). You should therefore simply accept it. – Clemens Jan 26 '17 at 15:11