private ExpressionObserver CreateDataContextSubject( IObservablePropertyBag target, string path, bool targetIsDataContext) { Contract.Requires <ArgumentNullException>(target != null); var dataContextHost = targetIsDataContext ? target.InheritanceParent as IObservablePropertyBag : target; if (dataContextHost != null) { var result = new ExpressionObserver( () => dataContextHost.GetValue(Control.DataContextProperty), path); dataContextHost.GetObservable(Control.DataContextProperty).Subscribe(x => result.UpdateRoot()); return(result); } else { throw new InvalidOperationException( "Cannot bind to DataContext of object with no parent."); } }
public void Can_Replace_Root() { var first = new Class1 { Foo = "foo" }; var second = new Class1 { Foo = "bar" }; var root = first; var target = new ExpressionObserver(() => root, "Foo"); var result = new List <object>(); var sub = target.Subscribe(x => result.Add(x)); root = second; target.UpdateRoot(); root = null; target.UpdateRoot(); Assert.Equal(new[] { "foo", "bar", PerspexProperty.UnsetValue }, result); Assert.Equal(0, first.SubscriptionCount); Assert.Equal(0, second.SubscriptionCount); }
public void Empty_Expression_Should_Track_Root() { var data = new Class1 { Foo = "foo" }; var target = new ExpressionObserver(() => data.Foo, ""); var result = new List <object>(); target.Subscribe(x => result.Add(x)); data.Foo = "bar"; target.UpdateRoot(); Assert.Equal(new[] { "foo", "bar" }, result); }
private ExpressionObserver CreateTemplatedParentExpressionSubject(IObservablePropertyBag target) { var result = new ExpressionObserver( () => target.GetValue(Control.TemplatedParentProperty), GetExpression()); if (target.GetValue(Control.TemplatedParentProperty) == null) { // TemplatedParent should only be set once, so only listen for the first non-null // value. target.GetObservable(Control.TemplatedParentProperty) .Where(x => x != null) .Take(1) .Subscribe(x => result.UpdateRoot()); } return(result); }
public ExpressionObserver CreateExpressionObserver( IObservablePropertyBag instance, PerspexProperty property) { var dataContextHost = property != Control.DataContextProperty ? instance : instance.InheritanceParent as IObservablePropertyBag; if (dataContextHost != null) { var result = new ExpressionObserver( () => dataContextHost.GetValue(Control.DataContextProperty), SourcePropertyPath); dataContextHost.GetObservable(Control.DataContextProperty).Subscribe(x => result.UpdateRoot()); return(result); } return(null); }
private ExpressionObserver CreateTemplatedParentSubject( IObservablePropertyBag target, string path) { Contract.Requires <ArgumentNullException>(target != null); var result = new ExpressionObserver( () => target.GetValue(Control.TemplatedParentProperty), path); if (target.GetValue(Control.TemplatedParentProperty) == null) { // TemplatedParent should only be set once, so only listen for the first non-null // value. target.GetObservable(Control.TemplatedParentProperty) .Where(x => x != null) .Take(1) .Subscribe(x => result.UpdateRoot()); } return(result); }
public void Can_Replace_Root() { var first = new Class1 { Foo = "foo" }; var second = new Class1 { Foo = "bar" }; var root = first; var target = new ExpressionObserver(() => root, "Foo"); var result = new List<object>(); var sub = target.Subscribe(x => result.Add(x)); root = second; target.UpdateRoot(); root = null; target.UpdateRoot(); Assert.Equal(new[] { "foo", "bar", PerspexProperty.UnsetValue }, result); Assert.Equal(0, first.SubscriptionCount); Assert.Equal(0, second.SubscriptionCount); }
public void Empty_Expression_Should_Track_Root() { var data = new Class1 { Foo = "foo" }; var target = new ExpressionObserver(() => data.Foo, ""); var result = new List<object>(); target.Subscribe(x => result.Add(x)); data.Foo = "bar"; target.UpdateRoot(); Assert.Equal(new[] { "foo", "bar" }, result); }
private ExpressionObserver CreateTemplatedParentSubject( IObservablePropertyBag target, string path) { Contract.Requires<ArgumentNullException>(target != null); var result = new ExpressionObserver( () => target.GetValue(Control.TemplatedParentProperty), path); if (target.GetValue(Control.TemplatedParentProperty) == null) { // TemplatedParent should only be set once, so only listen for the first non-null // value. target.GetObservable(Control.TemplatedParentProperty) .Where(x => x != null) .Take(1) .Subscribe(x => result.UpdateRoot()); } return result; }
private ExpressionObserver CreateDataContextSubject( IObservablePropertyBag target, string path, bool targetIsDataContext) { Contract.Requires<ArgumentNullException>(target != null); if (!targetIsDataContext) { var result = new ExpressionObserver( () => target.GetValue(Control.DataContextProperty), path); /// TODO: Instead of doing this, make the ExpressionObserver accept an "update" /// observable as doing it this way can will cause a leak in Binding as this /// observable is never unsubscribed. target.GetObservable(Control.DataContextProperty).Subscribe(x => result.UpdateRoot()); return result; } else { return new ExpressionObserver( target.GetObservable(Visual.VisualParentProperty) .OfType<IObservablePropertyBag>() .Select(x => x.GetObservable(Control.DataContextProperty)) .Switch(), path); } }
private ExpressionObserver CreateTemplatedParentExpressionSubject(IObservablePropertyBag target) { var result = new ExpressionObserver( () => target.GetValue(Control.TemplatedParentProperty), GetExpression()); if (target.GetValue(Control.TemplatedParentProperty) == null) { // TemplatedParent should only be set once, so only listen for the first non-null // value. target.GetObservable(Control.TemplatedParentProperty) .Where(x => x != null) .Take(1) .Subscribe(x => result.UpdateRoot()); } return result; }
private ExpressionObserver CreateDataContextExpressionSubject( IObservablePropertyBag target, bool targetIsDataContext) { var dataContextHost = targetIsDataContext ? target.InheritanceParent as IObservablePropertyBag : target; if (dataContextHost != null) { var result = new ExpressionObserver( () => dataContextHost.GetValue(Control.DataContextProperty), GetExpression()); dataContextHost.GetObservable(Control.DataContextProperty).Subscribe(x => result.UpdateRoot()); return result; } else { throw new InvalidOperationException( "Cannot bind to DataContext of object with no parent."); } }
public ExpressionObserver CreateDataContextExpressionSubject( IObservablePropertyBag instance, PerspexProperty property) { var dataContextHost = property != Control.DataContextProperty ? instance : instance.InheritanceParent as IObservablePropertyBag; if (dataContextHost != null) { var result = new ExpressionObserver( () => dataContextHost.GetValue(Control.DataContextProperty), GetExpression()); dataContextHost.GetObservable(Control.DataContextProperty).Subscribe(x => result.UpdateRoot()); return result; } return null; }