WPF:显示属性更改而不实现 INotifyPropertyChanged 接口

发布于 2024-08-20 04:40:15 字数 355 浏览 3 评论 0原文

在我的 WPF 应用程序中,我有一个布尔属性,我想向用户显示该属性(例如带有只读复选框)。通常我会实现 INotifyPropertyChanged,以便 WPF 可以对其采取行动并相应地更改复选框。

我现在遇到的问题是该属性值是从封闭框架中检索的。这意味着,我只能轮询该值,但没有可以订阅的更改事件。

我的第一个想法是创建一个单独的线程,它定期(例如每 10 毫秒)轮询该值并在该值发生更改时创建一个事件。但这对我来说似乎太过分了。

所以我的问题是:WPF 中是否有一个功能可以显示不断变化的值,而 INotifyPropertyChanged 不是一个选项?也许是某种轮询机制?如果没有,您将如何解决这个问题?

感谢您抽出时间。

In my WPF application, I have a boolean property which I would like to show to the user (for example with a read-only checkbox). Normally I would implement INotifyPropertyChanged so WPF can act on that and change the checkbox accordingly.

The problem I am having right now is that this property value is retrieved from a closed framework. That means, I can only poll the value, but there is no change-event I can subscribe to.

My first thought was to create a seperate thread, which periodically (say every 10 milliseconds) polls the value and creates an event when the value has changed. But that seems like overkill to me.

So my question is: is there a feature in WPF for displaying changing values where INotifyPropertyChanged is not an option? Some sort of poll mechanism, maybe? If not, how would you tackle this problem?

Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷遇一生 2024-08-27 04:40:15

如果该值来自您无法控制的某个地方,请为相关对象创建一个“ViewModel”并自行处理。

public class ClosedSourceObjectViewModel : ViewModelBase
{
    private ClosedSourceObject ClosedSourceObject
    {
        get;
        set;
    }

    public bool SomeProperty
    {
        get { return this.ClosedSourceObject.SomeProperty; }
        set
        {
            if (value != this.ClosedSourceObject.SomeProperty)
            {
                RaisePropertyChanging("SomeProperty");
                this.ClosedSourceObject.SomeProperty = value;
                RaisePropertyChanged("SomeProperty");
            }
        }
    }
}

If the value comes from somewhere you cannot control, create a "ViewModel" for the object in question and handle that yourself.

public class ClosedSourceObjectViewModel : ViewModelBase
{
    private ClosedSourceObject ClosedSourceObject
    {
        get;
        set;
    }

    public bool SomeProperty
    {
        get { return this.ClosedSourceObject.SomeProperty; }
        set
        {
            if (value != this.ClosedSourceObject.SomeProperty)
            {
                RaisePropertyChanging("SomeProperty");
                this.ClosedSourceObject.SomeProperty = value;
                RaisePropertyChanged("SomeProperty");
            }
        }
    }
}
孤寂小茶 2024-08-27 04:40:15

我同意@Alastair。因为

(1) 您希望从封闭框架中检索值,而该框架不会通知您有关属性更改的信息。
(2) 您可以轮询该值,但您不想这样做!

我认为没有其他方法可以做到这一点:(

I agree with @Alastair. Because

(1) You want to retrieve value from a closed framework that does not notify you about property change.
(2) You can poll the value, but you don't want to do that!

I don't think there'll be any other way of doing this :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文