通知静态类中静态属性的绑定 [英] Notify binding for static properties in static classes

查看:24
本文介绍了通知静态类中静态属性的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 static 类中找到了 static 属性的通知属性更改示例.但它不会更新 TextBlock 中的任何更改.这是代码.

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.

第一个绑定在构造函数中使用test"字符串,但 StaticPropertyChanged 始终为空.

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

在视图中...

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

无论我在哪里更改属性,TextBlock 都不会更新.

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

欣赏

推荐答案

类似于 INotifyPropertyChanged 的​​实现,静态属性更改通知 仅当您在触发 StaticPropertyChanged 事件时使用正确的属性名称时才有效.

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");
    }
}

绑定应该是这样的:

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

这个博客文章,了解有关静态属性更改通知的详细信息.

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

您也可以通过使用CallerMemberNameAttribute 来完全避免编写属性名称:

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

这篇关于通知静态类中静态属性的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆