Пример #1
0
 public void Start(bool hasActivator)
 {
     if (hasActivator)
     {
         if (_styledProperty is object)
         {
             _subscription = _target.Bind(_styledProperty, this, BindingPriority.StyleTrigger);
         }
         else
         {
             _subscription = _target.Bind(_directProperty, this);
         }
     }
     else
     {
         if (_styledProperty is object)
         {
             _subscription = _target.SetValue(_styledProperty, _value, BindingPriority.Style);
         }
         else
         {
             _target.SetValue(_directProperty !, _value);
         }
     }
 }
Пример #2
0
 /// <summary>
 /// Applies the setter to the 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 void Apply(IStyle style, IStyleable control, IObservable <bool> activator)
 {
     if (activator == null)
     {
         control.Bind(Property, Source, BindingPriority.Style);
     }
     else
     {
         var binding = new StyleBinding(activator, Source, style.ToString());
         control.Bind(Property, binding, BindingPriority.StyleTrigger);
     }
 }
Пример #3
0
 /// <summary>
 /// Applies the setter to the 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 void Apply(IStyle style, IStyleable control, IObservable<bool> activator)
 {
     if (activator == null)
     {
         control.Bind(Property, Source, BindingPriority.Style);
     }
     else
     {
         var binding = new StyleBinding(activator, Source, style.ToString());
         control.Bind(Property, binding, BindingPriority.StyleTrigger);
     }
 }
Пример #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 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);
        }
        public void Start(bool hasActivator)
        {
            _isActive = !hasActivator;

            if (_styledProperty is object)
            {
                var priority = hasActivator ? BindingPriority.StyleTrigger : BindingPriority.Style;
                _subscription = _target.Bind(_styledProperty, this, priority);
            }
            else
            {
                _subscription = _target.Bind(_directProperty, this);
            }
        }
Пример #6
0
        public void Start(bool hasActivator)
        {
            _isActive = !hasActivator;

            if (_styledProperty is object)
            {
                if (_binding.Mode != BindingMode.OneWayToSource)
                {
                    var priority = hasActivator ? BindingPriority.StyleTrigger : BindingPriority.Style;
                    _subscription = _target.Bind(_styledProperty, this, priority);
                }

                if (_binding.Mode == BindingMode.TwoWay)
                {
                    _subscriptionTwoWay = _target.GetBindingObservable(_styledProperty).Subscribe(this);
                }
            }
            else
            {
                if (_binding.Mode != BindingMode.OneWayToSource)
                {
                    _subscription = _target.Bind(_directProperty !, this);
                }

                if (_binding.Mode == BindingMode.TwoWay)
                {
                    _subscriptionTwoWay = _target.GetBindingObservable(_directProperty !).Subscribe(this);
                }
            }
        }
        public void Start(bool hasActivator)
        {
            if (hasActivator)
            {
                if (_styledProperty is not null)
                {
                    _subscription = _target.Bind(_styledProperty, this, BindingPriority.StyleTrigger);
                }
                else
                {
                    _subscription = _target.Bind(_directProperty !, this);
                }
            }
            else
            {
                var target = (AvaloniaObject)_target;

                if (_styledProperty is not null)
                {
                    _subscription = target.SetValue(_styledProperty !, _value, BindingPriority.Style);
                }
                else
                {
                    target.SetValue(_directProperty !, _value);
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Applies the setter to the 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 void 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)
                {
                    control.Bind(Property, binding);
                }
                else
                {
                    var subject   = binding.CreateSubject(control, Property);
                    var activated = new ActivatedSubject(activator, subject, description);
                    Bind(control, Property, binding, activated);
                }
            }
            else
            {
                if (activator == null)
                {
                    control.SetValue(Property, Value, BindingPriority.Style);
                }
                else
                {
                    var activated = new ActivatedValue(activator, Value, description);
                    control.Bind(Property, activated, BindingPriority.StyleTrigger);
                }
            }
        }
Пример #9
0
        public void Attach(IStyleable control)
        {
            var description = "Style " + this.Selector.ToString();
            var match = this.Selector.Match(control);

            if (match.ImmediateResult.HasValue)
            {
                if (match.ImmediateResult == true)
                {
                    foreach (Setter setter in this.Setters)
                    {
                        if (setter.Source != null && setter.Value != null)
                        {
                            throw new InvalidOperationException("Cannot set both Source and Value on a Setter.");
                        }

                        if (setter.Source == null)
                        {
                            control.SetValue(setter.Property, setter.Value, BindingPriority.Style);
                        }
                        else
                        {
                            control.Bind(setter.Property, setter.Source, BindingPriority.Style);
                        }
                    }
                }
            }
            else
            {
                foreach (Setter setter in this.Setters)
                {
                    if (setter.Source != null && setter.Value != null)
                    {
                        throw new InvalidOperationException("Cannot set both Source and Value on a Setter.");
                    }

                    StyleBinding binding;

                    if (setter.Source == null)
                    {
                        binding = new StyleBinding(match.ObservableResult, setter.Value, description);
                    }
                    else
                    {
                        binding = new StyleBinding(match.ObservableResult, setter.Source, description);
                    }

                    control.Bind(setter.Property, binding, BindingPriority.StyleTrigger);
                }
            }
        }
Пример #10
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);
        }
Пример #11
0
        private void Bind(
            IStyleable control,
            PerspexProperty property,
            IBinding binding,
            ISubject <object> subject)
        {
            var mode = binding.Mode;

            if (mode == BindingMode.Default)
            {
                mode = property.GetMetadata(control.GetType()).DefaultBindingMode;
            }

            control.Bind(
                property,
                subject,
                mode,
                binding.Priority);
        }
Пример #12
0
        /// <summary>
        /// Applies the setter to the 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 void 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)
                {
                    control.Bind(Property, binding);
                }
                else
                {
                    var subject = binding.CreateSubject(control, Property);
                    var activated = new ActivatedSubject(activator, subject, description);
                    Bind(control, Property, binding, activated);
                }
            }
            else
            {
                if (activator == null)
                {
                    control.SetValue(Property, Value, BindingPriority.Style);
                }
                else
                {
                    var activated = new ActivatedValue(activator, Value, description);
                    control.Bind(Property, activated, BindingPriority.StyleTrigger);
                }
            }
        }
Пример #13
0
        public void Attach(IStyleable control)
        {
            string         description = "Style " + this.Selector.ToString();
            StyleActivator activator   = this.Selector.GetActivator(control);

            if (activator.CurrentValue || !activator.HasCompleted)
            {
                IObservable <bool> observable = activator;

                // If the activator has completed, then we want its value to be true forever.
                // Because of this we can't pass the activator directly as it will complete
                // immediately and remove the binding.
                if (activator.HasCompleted)
                {
                    observable = Observable.Never <bool>().StartWith(true);
                }

                foreach (Setter setter in this.Setters)
                {
                    StyleBinding binding = new StyleBinding(observable, setter.Value, description);
                    control.Bind(setter.Property, binding, this.Selector.Priority);
                }
            }
        }
Пример #14
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;

                if (template != null)
                {
                    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;
        }
Пример #15
0
        private void Bind(
            IStyleable control,
            PerspexProperty property,
            IBinding binding,
            ISubject<object> subject)
        {
            var mode = binding.Mode;

            if (mode == BindingMode.Default)
            {
                mode = property.GetMetadata(control.GetType()).DefaultBindingMode;
            }

            control.Bind(
                property,
                subject,
                mode,
                binding.Priority);
        }