Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

c# - Notify binding for static properties in static classes

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...