开发者

WPF: Show Property Change without implementing INotifyPropertyChanged interface

开发者 https://www.devze.com 2022-12-19 03:17 出处:网络
In my WPF application, I have a boolean property which I would like to show to the user (for example with a re开发者_C百科ad-only checkbox). Normally I would implement INotifyPropertyChanged so WPF ca

In my WPF application, I have a boolean property which I would like to show to the user (for example with a re开发者_C百科ad-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.


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");
            }
        }
    }
}


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 :(

0

精彩评论

暂无评论...
验证码 换一张
取 消