Exemple #1
0
        /// <summary>
        /// Applies the setter to a control.
        /// </summary>
        /// <param name="style">The style that is being applied.</param>
        /// <param name="control">The control.</param>
        /// <param name="activator">An optional activator.</param>
        public IDisposable Apply(IStyle style, IStyleable control, IObservable <bool> activator)
        {
            Contract.Requires <ArgumentNullException>(control != null);

            var description = style?.ToString();

            if (Property == null)
            {
                throw new InvalidOperationException("Setter.Property must be set.");
            }

            var value   = Value;
            var binding = value as IBinding;

            if (binding == null)
            {
                var  template = value as ITemplate;
                bool isPropertyOfTypeITemplate = typeof(ITemplate).GetTypeInfo()
                                                 .IsAssignableFrom(Property.PropertyType.GetTypeInfo());

                if (template != null && !isPropertyOfTypeITemplate)
                {
                    var materialized = template.Build();
                    NameScope.SetNameScope((Visual)materialized, new NameScope());
                    value = materialized;
                }

                if (activator == null)
                {
                    return(control.Bind(Property, ObservableEx.SingleValue(value), BindingPriority.Style));
                }
                else
                {
                    var activated = new ActivatedValue(activator, value, description);
                    return(control.Bind(Property, activated, BindingPriority.StyleTrigger));
                }
            }
            else
            {
                var source = binding.Initiate(control, Property);

                if (source != null)
                {
                    var cloned = Clone(source, style, activator);
                    return(BindingOperations.Apply(control, Property, cloned, null));
                }
            }

            return(Disposable.Empty);
        }
Exemple #2
0
        private InstancedBinding Clone(InstancedBinding sourceInstance, IStyle style, IObservable <bool> activator)
        {
            if (activator != null)
            {
                var description = style?.ToString();

                switch (sourceInstance.Mode)
                {
                case BindingMode.OneTime:
                    if (sourceInstance.Observable != null)
                    {
                        var activated = new ActivatedObservable(activator, sourceInstance.Observable, description);
                        return(InstancedBinding.OneTime(activated, BindingPriority.StyleTrigger));
                    }
                    else
                    {
                        var activated = new ActivatedValue(activator, sourceInstance.Value, description);
                        return(InstancedBinding.OneTime(activated, BindingPriority.StyleTrigger));
                    }

                case BindingMode.OneWay:
                {
                    var activated = new ActivatedObservable(activator, sourceInstance.Observable, description);
                    return(InstancedBinding.OneWay(activated, BindingPriority.StyleTrigger));
                }

                case BindingMode.OneWayToSource:
                {
                    var activated = new ActivatedSubject(activator, sourceInstance.Subject, description);
                    return(InstancedBinding.OneWayToSource(activated, BindingPriority.StyleTrigger));
                }

                case BindingMode.TwoWay:
                {
                    var activated = new ActivatedSubject(activator, sourceInstance.Subject, description);
                    return(InstancedBinding.TwoWay(activated, BindingPriority.StyleTrigger));
                }

                default:
                    throw new NotSupportedException("Unsupported BindingMode.");
                }
            }
            else
            {
                return(sourceInstance.WithPriority(BindingPriority.Style));
            }
        }
Exemple #3
0
        private InstancedBinding Clone(InstancedBinding sourceInstance, IStyle style, IObservable <bool> activator)
        {
            InstancedBinding cloned;

            if (activator != null)
            {
                var description = style?.ToString();

                if (sourceInstance.Mode == BindingMode.TwoWay || sourceInstance.Mode == BindingMode.OneWayToSource)
                {
                    var activated = new ActivatedSubject(activator, sourceInstance.Subject, description);
                    cloned = new InstancedBinding(activated, sourceInstance.Mode, BindingPriority.StyleTrigger);
                }
                else if (sourceInstance.Mode == BindingMode.OneTime)
                {
                    var activated = new ActivatedValue(activator, sourceInstance.Value, description);
                    cloned = new InstancedBinding(activated, BindingMode.OneWay, BindingPriority.StyleTrigger);
                }
                else
                {
                    var activated = new ActivatedObservable(activator, sourceInstance.Observable ?? sourceInstance.Subject, description);
                    cloned = new InstancedBinding(activated, sourceInstance.Mode, BindingPriority.StyleTrigger);
                }
            }
            else
            {
                if (sourceInstance.Subject != null)
                {
                    cloned = new InstancedBinding(sourceInstance.Subject, sourceInstance.Mode, BindingPriority.Style);
                }
                else if (sourceInstance.Observable != null)
                {
                    cloned = new InstancedBinding(sourceInstance.Observable, sourceInstance.Mode, BindingPriority.Style);
                }
                else
                {
                    cloned = new InstancedBinding(sourceInstance.Value, BindingPriority.Style);
                }
            }

            return(cloned);
        }
Exemple #4
0
        /// <summary>
        /// Applies the setter to a control.
        /// </summary>
        /// <param name="style">The style that is being applied.</param>
        /// <param name="control">The control.</param>
        /// <param name="activator">An optional activator.</param>
        public IDisposable Apply(IStyle style, IStyleable control, IObservable <bool> activator)
        {
            Contract.Requires <ArgumentNullException>(control != null);

            var description = style?.ToString();

            if (Property == null)
            {
                throw new InvalidOperationException("Setter.Property must be set.");
            }

            var binding = Value as IBinding;

            if (binding == null)
            {
                if (activator == null)
                {
                    return(control.Bind(Property, ObservableEx.SingleValue(Value), BindingPriority.Style));
                }
                else
                {
                    var activated = new ActivatedValue(activator, Value, description);
                    return(control.Bind(Property, activated, BindingPriority.StyleTrigger));
                }
            }
            else
            {
                var source = binding.Initiate(control, Property);

                if (source != null)
                {
                    var cloned = Clone(source, style, activator);
                    return(BindingOperations.Apply(control, Property, cloned, null));
                }
            }

            return(Disposable.Empty);
        }