绑定静态属性并实现INotifyPropertyChanged

编程入门 行业动态 更新时间:2024-10-09 18:19:47
本文介绍了绑定静态属性并实现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

本文发布于:2023-11-05 10:06:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560550.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   静态   属性   INotifyPropertyChanged

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!