绑定静态属性并实现 INotifyPropertyChanged [英] Binding static property and implementing INotifyPropertyChanged

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

问题描述

我正在尝试将某个类的静态属性绑定到某个控件.我尝试了一些实现,但每个实现都有其问题:

I'm trying to bind a static property of some class to some control. I've tryied a few implementation but each has its problem:

所有示例都使用下一个 XAML:

All examples use the next XAML:

 <Label Name="label1" Content="{Binding Path=text}"/>  

第一种方法 - 不要使用 INotifyPropertyChanged

1st approach - don't use INotifyPropertyChanged

public class foo1
{
    public static string text { get; set; }
}

问题在于,当文本"属性更改时,不会通知控件.

The problem is that when 'text' propery changes the control is not notified.

第二种方法 - 使用 INotifyPropertyChanged

Second approach - use INotifyPropertyChanged

public class foo1 : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private static string _text;
    public static string text
    {
        get { return _text; }
        set
        {
            _text = value;
            OnPropertyChanged("text");
        }
    }
}

这不会编译,因为 OnPropertyChanged() 方法不是静态的,它是在静态方法中调用的.

This doesn't compile because OnPropertyChanged() method is not static and it's called within a static method.

第二种方法尝试 2:使 OnPropertyChanged() 方法静态 => 这不会编译,因为 OnPropertyChanged() 现在是静态的,它尝试使用非静态的 'PropertyChanged' 事件.

Second approach try 2: make OnPropertyChanged() method static => this doesn't compile because OnPropertyChanged() is now static and it tries to use 'PropertyChanged' event which is not static.

第二种方法尝试 3:使 'PropertyChanged' 事件静态 => 这不会编译,因为该类没有实现 'INotifyPropertyChanged.PropertyChanged' 事件(该事件在 'INotifyPropertyChanged 接口中定义不是静态的,但在这里它是静态的).

Second approach try 3: make 'PropertyChanged' event static => this doesn't compile because the class does not implement 'INotifyPropertyChanged.PropertyChanged' event (the event is defined in 'INotifyPropertyChanged interface is not static but here it is static).

此时我放弃了.

有什么想法吗?

推荐答案

我建议你只需要一个实例属性返回你的静态属性,就像这样:

I'd suggest you just have an instance-property return your static property like this:

private static string _text;
public string text
{
    get { return _text; }
    set
    {
        _text = value;
        OnPropertyChanged("text");
    }
}

然而,这使得整个绑定相对而言毫无意义,因为更改通知仅在类的一个实例中创建,而不是在每个实例中创建.因此,只有绑定到更改它的特定实例上的属性的绑定才会更新.

However this makes the whole binding comparatively pointless since change notifications are only created in one instance of the class and not every instance. Thus only bindings which bind to the property on the specific instance on which it was changed will update.

更好的方法是使用单例,如此处所示.

A better method would be using a singleton as can be seen here.

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

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