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

查看:589
本文介绍了绑定静态属性并实现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; }
}

问题是当'text'属性更改时,控件没有被通知

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:make OnPropertyChanged()方法static =>这不编译,因为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:make'PropertyChanged'event static => this doesn' t编译,因为类不会实现'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).

点我放弃了。

任何想法?

推荐答案

我建议你只需要一个instance-property返回你的静态属性,如下所示:

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天全站免登陆