コード例 #1
0
ファイル: Binding.cs プロジェクト: Serg2DFX/Perspex
        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.");
            }
        }
コード例 #2
0
        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);
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #5
0
ファイル: XamlBinding.cs プロジェクト: rdterner/Perspex
        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);
        }
コード例 #6
0
ファイル: Binding.cs プロジェクト: Serg2DFX/Perspex
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
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);
        }
コード例 #9
0
ファイル: Binding.cs プロジェクト: Sergey-Terekhin/Perspex
        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;
        }
コード例 #10
0
ファイル: Binding.cs プロジェクト: Sergey-Terekhin/Perspex
        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);
            }
        }
コード例 #11
0
ファイル: Binding.cs プロジェクト: JackWangCUMT/Perspex
        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;
        }
コード例 #12
0
ファイル: Binding.cs プロジェクト: JackWangCUMT/Perspex
        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.");
            }
        }
コード例 #13
0
ファイル: Binding.cs プロジェクト: strogo/Perspex
        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;
        }