takayoshi
takayoshi

Reputation: 2799

dependency property to observable

I'm fan of Reactive Extension and especially ReactiveUI I have DP in other solution's project than mine. I'd like to convert this into observable Class containing this DP is internal and derived from DependencyObject thus I cannot use Class.ObservableFromDP because class must be derived from FrameworkElement

I have this solution

public static IObservable<T> ToObservable<T>(this DependencyObject dependencyObject, DependencyProperty property)
    {
        return Observable.Create<T>(o =>
                                        {
                                            var des = DependencyPropertyDescriptor.FromProperty(property,
                                                                                                dependencyObject.
                                                                                                    GetType());
                                            var eh =
                                                new EventHandler(
                                                    (s, e) => o.OnNext((T) des.GetValue(dependencyObject)));
                                            des.AddValueChanged(dependencyObject, eh);
                                            return () => des.RemoveValueChanged(dependencyObject, eh);
                                        });

But target class is internal, I cannot access property DependencyProperty in this class

How can I get Observable from this property

Is there any method as

obj.ObservableFromDP(x=>x.ActiveEditor) working on obj not derived from FrameworkElement?

Upvotes: 2

Views: 1523

Answers (1)

Ana Betts
Ana Betts

Reputation: 74654

This is actually fixed in >= ReactiveUI 4.0. Now all you do is:

// WhenAny now works on any object, will detect DependencyObject automatically
obj.WhenAny(x => x.ActiveEditor, x => x.Value)
   .Subscribe(/* ... */)

Upvotes: 3

Related Questions