views:

352

answers:

2

Hi!

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.

A: 

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

Mihir Gokani
Why are you discouraging polling? The property is read-only and changes don't need to be reflected immediately, a short delay is fine.
Maximilian Csuk
@Max: I don't see it as discouraging polling. He's agreeing that without polling, it's impossible. The only way to do it is polling.
Joel B Fant
OK, thanks for your time. :-) Then polling it is...
Maximilian Csuk
I'm not *discouraging* polling and I don't know why do you feel so! I was trying to say that essentially you'll need to periodically poll the value and if the value is changed then you'll have to notify this change using `PropertyChanged` event (of INotifyPropertyChanged interface) - and that you don't want to do! **Note one thing** : Polling and `PropertyChanged` will go hand in hand. If you are polling periodically, you should also raise `PropertyChanged` simultaneously to notify about the change (if the value is changed). Sorry for not clarifying the things in the answer :<
Mihir Gokani
Also sorry for the misunderstanding. The "You can poll the value, but you don't want to do that!" threw me off. It's clear to me now.
Maximilian Csuk
+3  A: 

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");
            }
        }
    }
}
sixlettervariables
If there are a lot of properties you could also generate a complete wrapper class to save you time.
smaclell
Indeed, note `ViewModelBase` contains implementations of `INotifyPropertyChanging` and `INotifyPropertyChanged`.
sixlettervariables
Thanks for your posts!That is indeed bad news. :-(The problem I am having with your approach is that my value is changed from within the closed framework, not by me. I am not able to change it at all.
Maximilian Csuk
Then apart from polling, you have no recourse.
sixlettervariables
OK, thanks for your time. :-) Then polling it is...
Maximilian Csuk