Exemple #1
0
        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);
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        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);
        }
Exemple #9
0
        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;
        }
Exemple #10
0
        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);
            }
        }
Exemple #11
0
        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;
        }
Exemple #12
0
        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.");
            }
        }
Exemple #13
0
        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;
        }