Example #1
0
        /// <summary>
        /// Applies a binding subject to a property on an instance.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="property">The target property.</param>
        /// <param name="subject">The binding subject.</param>
        internal void Bind(IObservablePropertyBag target, PerspexProperty property, ISubject <object> subject)
        {
            var mode = Mode == BindingMode.Default ?
                       property.DefaultBindingMode : Mode;

            switch (mode)
            {
            case BindingMode.Default:
            case BindingMode.OneWay:
                target.Bind(property, subject, Priority);
                break;

            case BindingMode.TwoWay:
                throw new NotSupportedException("TwoWay MultiBinding not currently supported.");

            case BindingMode.OneTime:
                target.GetObservable(Control.DataContextProperty).Subscribe(dataContext =>
                {
                    subject.Take(1).Subscribe(x => target.SetValue(property, x, Priority));
                });
                break;

            case BindingMode.OneWayToSource:
                target.GetObservable(property).Subscribe(subject);
                break;
            }
        }
Example #2
0
        /// <summary>
        /// Applies a binding subject to a property on an instance.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="property">The target property.</param>
        /// <param name="subject">The binding subject.</param>
        internal void Bind(IObservablePropertyBag target, PerspexProperty property, ISubject <object> subject)
        {
            Contract.Requires <ArgumentNullException>(target != null);
            Contract.Requires <ArgumentNullException>(property != null);
            Contract.Requires <ArgumentNullException>(subject != null);

            var mode = Mode == BindingMode.Default ?
                       property.DefaultBindingMode : Mode;

            switch (mode)
            {
            case BindingMode.Default:
            case BindingMode.OneWay:
                target.Bind(property, subject, Priority);
                break;

            case BindingMode.TwoWay:
                target.BindTwoWay(property, subject, Priority);
                break;

            case BindingMode.OneTime:
                target.GetObservable(Control.DataContextProperty).Subscribe(dataContext =>
                {
                    subject.Take(1).Subscribe(x => target.SetValue(property, x, Priority));
                });
                break;

            case BindingMode.OneWayToSource:
                target.GetObservable(property).Subscribe(subject);
                break;
            }
        }
Example #3
0
        private ExpressionObserver CreateDataContextSubject(
            IObservablePropertyBag target,
            string path,
            bool targetIsDataContext)
        {
            Contract.Requires <ArgumentNullException>(target != null);

            if (!targetIsDataContext)
            {
                var update = target.GetObservable(Control.DataContextProperty)
                             .Skip(1)
                             .Select(_ => Unit.Default);
                var result = new ExpressionObserver(
                    () => target.GetValue(Control.DataContextProperty),
                    path,
                    update);

                return(result);
            }
            else
            {
                return(new ExpressionObserver(
                           target.GetObservable(Visual.VisualParentProperty)
                           .OfType <IObservablePropertyBag>()
                           .Select(x => x.GetObservable(Control.DataContextProperty))
                           .Switch(),
                           path));
            }
        }
Example #4
0
        internal void Bind(IObservablePropertyBag target, PerspexProperty property, ISubject <object> subject)
        {
            var mode = BindingMode == BindingMode.Default ?
                       property.DefaultBindingMode : BindingMode;

            switch (mode)
            {
            case BindingMode.Default:
            case BindingMode.OneWay:
                target.Bind(property, subject);
                break;

            case BindingMode.TwoWay:
                target.BindTwoWay(property, subject);
                break;

            case BindingMode.OneTime:
                target.GetObservable(Control.DataContextProperty).Subscribe(dataContext =>
                {
                    subject.Take(1).Subscribe(x => target.SetValue(property, x));
                });
                break;

            case BindingMode.OneWayToSource:
                target.GetObservable(property).Subscribe(subject);
                break;
            }
        }
Example #5
0
        internal void Bind(IObservablePropertyBag target, PerspexProperty property, ISubject<object> subject)
        {
            var mode = BindingMode == BindingMode.Default ?
                property.DefaultBindingMode : BindingMode;

            switch (mode)
            {
                case BindingMode.Default:
                case BindingMode.OneWay:
                    target.Bind(property, subject);
                    break;
                case BindingMode.TwoWay:
                    target.BindTwoWay(property, subject);
                    break;
                case BindingMode.OneTime:
                    target.GetObservable(Control.DataContextProperty).Subscribe(dataContext =>
                    {
                        subject.Take(1).Subscribe(x => target.SetValue(property, x));
                    });                    
                    break;
                case BindingMode.OneWayToSource:
                    target.GetObservable(property).Subscribe(subject);
                    break;
            }
        }
Example #6
0
        public ExpressionObserver CreateExpressionObserver(
            IObservablePropertyBag instance, 
            PerspexProperty property)
        {
            IObservable<object> dataContext = null;

            if (property != Control.DataContextProperty)
            {
                dataContext = instance.GetObservable(Control.DataContextProperty);
            }
            else
            {
                var parent = instance.InheritanceParent as IObservablePropertyBag;

                if (parent != null)
                {
                    dataContext = parent.GetObservable(Control.DataContextProperty);
                }
            }

            if (dataContext != null)
            {
                var result = new ExpressionObserver(null, SourcePropertyPath);
                dataContext.Subscribe(x => result.Root = x);
                return result;
            }

            return null;
        }
Example #7
0
        internal void Bind(IObservablePropertyBag target, PerspexProperty property, ISubject<object> subject)
        {
            var mode = Mode == BindingMode.Default ?
                property.DefaultBindingMode : Mode;

            switch (mode)
            {
                case BindingMode.Default:
                case BindingMode.OneWay:
                    target.Bind(property, subject, Priority);
                    break;
                case BindingMode.TwoWay:
                    throw new NotSupportedException("TwoWay MultiBinding not currently supported.");
                case BindingMode.OneTime:
                    target.GetObservable(Control.DataContextProperty).Subscribe(dataContext =>
                    {
                        subject.Take(1).Subscribe(x => target.SetValue(property, x, Priority));
                    });                    
                    break;
                case BindingMode.OneWayToSource:
                    target.GetObservable(property).Subscribe(subject);
                    break;
            }
        }
Example #8
0
        private ExpressionObserver CreateTemplatedParentSubject(
            IObservablePropertyBag target,
            string path)
        {
            Contract.Requires <ArgumentNullException>(target != null);

            var update = target.GetObservable(Control.TemplatedParentProperty)
                         .Skip(1)
                         .Select(_ => Unit.Default);

            var result = new ExpressionObserver(
                () => target.GetValue(Control.TemplatedParentProperty),
                path,
                update);

            return(result);
        }
Example #9
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);
        }
Example #10
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);
        }
Example #11
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;
        }
Example #12
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);
            }
        }
Example #13
0
        /// <summary>
        /// Applies a binding subject to a property on an instance.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="property">The target property.</param>
        /// <param name="subject">The binding subject.</param>
        internal void Bind(IObservablePropertyBag target, PerspexProperty property, ISubject<object> subject)
        {
            Contract.Requires<ArgumentNullException>(target != null);
            Contract.Requires<ArgumentNullException>(property != null);
            Contract.Requires<ArgumentNullException>(subject != null);

            var mode = Mode == BindingMode.Default ?
                property.DefaultBindingMode : Mode;

            switch (mode)
            {
                case BindingMode.Default:
                case BindingMode.OneWay:
                    target.Bind(property, subject, Priority);
                    break;
                case BindingMode.TwoWay:
                    target.BindTwoWay(property, subject, Priority);
                    break;
                case BindingMode.OneTime:
                    target.GetObservable(Control.DataContextProperty).Subscribe(dataContext =>
                    {
                        subject.Take(1).Subscribe(x => target.SetValue(property, x, Priority));
                    });
                    break;
                case BindingMode.OneWayToSource:
                    target.GetObservable(property).Subscribe(subject);
                    break;
            }
        }
Example #14
0
        private ExpressionObserver CreateTemplatedParentSubject(
            IObservablePropertyBag target,
            string path)
        {
            Contract.Requires<ArgumentNullException>(target != null);

            var update = target.GetObservable(Control.TemplatedParentProperty)
                .Skip(1)
                .Select(_ => Unit.Default);

            var result = new ExpressionObserver(
                () => target.GetValue(Control.TemplatedParentProperty),
                path,
                update);

            return result;
        }
Example #15
0
        private ExpressionObserver CreateDataContextSubject(
            IObservablePropertyBag target,
            string path,
            bool targetIsDataContext)
        {
            Contract.Requires<ArgumentNullException>(target != null);

            if (!targetIsDataContext)
            {
                var update = target.GetObservable(Control.DataContextProperty)
                    .Skip(1)
                    .Select(_ => Unit.Default);
                var result = new ExpressionObserver(
                    () => target.GetValue(Control.DataContextProperty),
                    path,
                    update);

                return result;
            }
            else
            {
                return new ExpressionObserver(
                    target.GetObservable(Visual.VisualParentProperty)
                          .OfType<IObservablePropertyBag>()
                          .Select(x => x.GetObservable(Control.DataContextProperty))
                          .Switch(),
                    path);
            }
        }
Example #16
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;
        }