实施INotifyProperty在WPF和Silverlight改变了静态属性 [英] Implement INotifyProperty changed on Static Property in WPF and Silverlight

查看:143
本文介绍了实施INotifyProperty在WPF和Silverlight改变了静态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在的问题是如何落实 INotifyPropertyChanged的的静态属性,因为您实现该事件不是静态的,不能由一个静态属性调用。此外,您不能绑定到在Silverlight静态属性。

The question is how to implement INotifyPropertyChanged on a static property because the event you implement is not static, and cannot be called by a static property. Also, you cannot bind to a static property in Silverlight.

我已经看到了这个问题,弹出的几个论坛,各种解决方案,其中没有一个是非常满意的。

I've seen this question pop up an a few forums with a variety of solutions, none of which were very satisfying.

嗯,我想我已经找到了一个完美的解决方案,但它是如此简单我觉得我必须缺少的东西。

Well, I think I've found an elegant solution, but it's so simple I feel like I must be missing something.

答案是,写访问一个静态变量,像这样一个非静态属性:

The answer is, to write a non-static property that accesses a static variable like so:

    private static double length;
    public double Length
    {
        get
        {
            return length;
        }
        set
        {
            length = value;
            NotifyPropertyChanged("Length");
        }
    }

我测试过它,它似乎工作得很好。我失去了一些东西?

I've tested it and it seems to work just fine. Am I missing something?

推荐答案

从技术上说,你还没有绑定到一个静态属性 - 要绑定的类的实例,它是使用静态字段作为后盾商店。这将工作,在一定程度上,但...

Technically, you're still not binding to a static property - you're binding to an instance of the class, which is using the static field as a backing store. This will work, to some extent, but...

没有与此一个根本性的问题 - 如果你有多个项目绑定到这个相同的后备存储(这看起来像是你试图,因为你故意使其静态),在 INotifyPropertyChanged的的通知只会发生在哪里,你的当前绑定的实例。

There is a fundamental problem with this - If you have more than one item bound to this same backing store (which seems like something you're attempting, since you're purposefully making it static), the INotifyPropertyChanged notifications will only happen on the instance where you are currently bound.

说,例如,你有两个用户控件,并肩而坐,双方必然要包含这个code一个视图模型。当控制A设置该属性,控制B就永远不会收到通知(因为它是一个的INotifyPropertyChanged的运行),所以会出现不同步的。

Say, for example, you had two UserControls, sitting side by side, both bound to a ViewModel containing this code. When control A sets this property, control B will never receive notifications (since it's A's INotifyPropertyChanged that runs), so it will appear out of sync.

如果你真的想尝试做这样的事情,你最好让你的后备存储通过您的视图模型类中使用的类实现INotifyPropertyChanged,和泡沫了财产了。这样一来,多个实例都会被告知正确的,你可以处理任何必要时可能发生的多线程/同步问题。

If you really want to try to do something like this, you're probably better off having your backing store use a class that implements INotifyPropertyChanged, and "bubble" up the property through your ViewModel class. This way, multiple instances would all be notified correctly, and you could handle any multithreading/synchronization issues that might occur if necessary.

另外,你可能要考虑使用单一实例属性(用一个实例字段),一个辛格尔顿内。这也将让你分享你的静态属性的通知。

Alternatively, you may want to consider using a single instance property (with an instance field), inside of a Singleton. This, too, would give you shared notifications of your "static" property.

这篇关于实施INotifyProperty在WPF和Silverlight改变了静态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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