コード例 #1
0
        public void ClearValue <T>(StyledPropertyBase <T> property)
        {
            property = property ?? throw new ArgumentNullException(nameof(property));
            VerifyAccess();

            _values?.ClearLocalValue(property);
        }
コード例 #2
0
        public T GetValue <T>(StyledPropertyBase <T> property)
        {
            property = property ?? throw new ArgumentNullException(nameof(property));
            VerifyAccess();

            return(GetValueOrInheritedOrDefault(property));
        }
コード例 #3
0
ファイル: AvaloniaObject.cs プロジェクト: josegomez/Avalonia
        private T GetValueOrInheritedOrDefault <T>(StyledPropertyBase <T> property)
        {
            var o        = this;
            var inherits = property.Inherits;
            var value    = default(T);

            while (o != null)
            {
                var values = o._values;

                if (values?.TryGetValue(property, out value) == true)
                {
                    return(value);
                }

                if (!inherits)
                {
                    break;
                }

                o = o.InheritanceParent as AvaloniaObject;
            }

            return(property.GetDefaultValue(GetType()));
        }
コード例 #4
0
ファイル: AvaloniaObject.cs プロジェクト: josegomez/Avalonia
        /// <summary>
        /// Sets a <see cref="AvaloniaProperty"/> value.
        /// </summary>
        /// <typeparam name="T">The type of the property.</typeparam>
        /// <param name="property">The property.</param>
        /// <param name="value">The value.</param>
        /// <param name="priority">The priority of the value.</param>
        public void SetValue <T>(
            StyledPropertyBase <T> property,
            T value,
            BindingPriority priority = BindingPriority.LocalValue)
        {
            property = property ?? throw new ArgumentNullException(nameof(property));
            VerifyAccess();

            LogPropertySet(property, value, priority);

            if (value is UnsetValueType)
            {
                if (priority == BindingPriority.LocalValue)
                {
                    Values.ClearLocalValue(property);
                }
                else
                {
                    throw new NotSupportedException(
                              "Cannot set property to Unset at non-local value priority.");
                }
            }
            else if (!(value is DoNothingType))
            {
                Values.SetValue(property, value, priority);
            }
        }
コード例 #5
0
ファイル: AvaloniaObject.cs プロジェクト: josegomez/Avalonia
        void IValueSink.ValueChanged <T>(
            StyledPropertyBase <T> property,
            BindingPriority priority,
            Optional <T> oldValue,
            BindingValue <T> newValue)
        {
            oldValue = oldValue.HasValue ? oldValue : GetInheritedOrDefault(property);
            newValue = newValue.HasValue ? newValue : newValue.WithValue(GetInheritedOrDefault(property));

            LogIfError(property, newValue);

            if (!EqualityComparer <T> .Default.Equals(oldValue.Value, newValue.Value))
            {
                RaisePropertyChanged(property, oldValue, newValue, priority);

                Logger.TryGet(LogEventLevel.Verbose)?.Log(
                    LogArea.Property,
                    this,
                    "{Property} changed from {$Old} to {$Value} with priority {Priority}",
                    property,
                    oldValue,
                    newValue,
                    (BindingPriority)priority);
            }
        }
コード例 #6
0
 void IValueSink.Completed <T>(
     StyledPropertyBase <T> property,
     IPriorityValueEntry entry,
     Optional <T> oldValue)
 {
     ((IValueSink)this).ValueChanged(property, BindingPriority.Unset, oldValue, default);
 }
コード例 #7
0
ファイル: AvaloniaObject.cs プロジェクト: josegomez/Avalonia
        private T GetInheritedOrDefault <T>(StyledPropertyBase <T> property)
        {
            if (property.Inherits && InheritanceParent is AvaloniaObject o)
            {
                return(o.GetValueOrInheritedOrDefault(property));
            }

            return(property.GetDefaultValue(GetType()));
        }
コード例 #8
0
ファイル: AvaloniaObject.cs プロジェクト: josegomez/Avalonia
        /// <summary>
        /// Binds a <see cref="AvaloniaProperty"/> to an observable.
        /// </summary>
        /// <typeparam name="T">The type of the property.</typeparam>
        /// <param name="property">The property.</param>
        /// <param name="source">The observable.</param>
        /// <param name="priority">The priority of the binding.</param>
        /// <returns>
        /// A disposable which can be used to terminate the binding.
        /// </returns>
        public IDisposable Bind <T>(
            StyledPropertyBase <T> property,
            IObservable <BindingValue <T> > source,
            BindingPriority priority = BindingPriority.LocalValue)
        {
            property = property ?? throw new ArgumentNullException(nameof(property));
            source   = source ?? throw new ArgumentNullException(nameof(source));
            VerifyAccess();

            return(Values.AddBinding(property, source, priority));
        }
コード例 #9
0
        public Optional <T> GetBaseValue <T>(StyledPropertyBase <T> property, BindingPriority maxPriority)
        {
            property = property ?? throw new ArgumentNullException(nameof(property));
            VerifyAccess();

            if (_values is object &&
                _values.TryGetValue(property, maxPriority, out var value))
            {
                return(value);
            }

            return(default);
コード例 #10
0
        public bool TryGetValue <T>(StyledPropertyBase <T> property, out T value)
        {
            if (_values.TryGetValue(property, out var slot))
            {
                var v = (IValue <T>)slot;

                if (v.Value.HasValue)
                {
                    value = v.Value.Value;
                    return(true);
                }
            }

            value = default !;
コード例 #11
0
        /// <summary>
        /// Binds a <see cref="AvaloniaProperty"/> to an observable.
        /// </summary>
        /// <typeparam name="T">The type of the property.</typeparam>
        /// <param name="target">The object.</param>
        /// <param name="property">The property.</param>
        /// <param name="source">The observable.</param>
        /// <param name="priority">The priority of the binding.</param>
        /// <returns>
        /// A disposable which can be used to terminate the binding.
        /// </returns>
        public static IDisposable Bind <T>(
            this IAvaloniaObject target,
            AvaloniaProperty <T> property,
            IObservable <BindingValue <T> > source,
            BindingPriority priority = BindingPriority.LocalValue)
        {
            target   = target ?? throw new ArgumentNullException(nameof(target));
            property = property ?? throw new ArgumentNullException(nameof(property));
            source   = source ?? throw new ArgumentNullException(nameof(source));

            return(property switch
            {
                StyledPropertyBase <T> styled => target.Bind(styled, source, priority),
                DirectPropertyBase <T> direct => target.Bind(direct, source),
                _ => throw new NotSupportedException("Unsupported AvaloniaProperty type."),
            });
コード例 #12
0
        public bool TryGetValue <T>(
            StyledPropertyBase <T> property,
            BindingPriority maxPriority,
            out T value)
        {
            if (_values.TryGetValue(property, out var slot))
            {
                var v = ((IValue <T>)slot).GetValue(maxPriority);

                if (v.HasValue)
                {
                    value = v.Value;
                    return(true);
                }
            }

            value = default !;
コード例 #13
0
ファイル: AvaloniaObject.cs プロジェクト: josegomez/Avalonia
        /// <summary>
        /// Called for each inherited property when the <see cref="InheritanceParent"/> changes.
        /// </summary>
        /// <typeparam name="T">The type of the property value.</typeparam>
        /// <param name="property">The property.</param>
        /// <param name="oldParent">The old inheritance parent.</param>
        internal void InheritanceParentChanged <T>(
            StyledPropertyBase <T> property,
            IAvaloniaObject oldParent)
        {
            var oldValue = oldParent switch
            {
                AvaloniaObject o => o.GetValueOrInheritedOrDefault(property),
                null => property.GetDefaultValue(GetType()),
                _ => oldParent.GetValue(property)
            };

            var newValue = GetInheritedOrDefault(property);

            if (!EqualityComparer <T> .Default.Equals(oldValue, newValue))
            {
                RaisePropertyChanged(property, oldValue, newValue);
            }
        }
コード例 #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StyledPropertyBase{T}"/> class.
 /// </summary>
 /// <param name="source">The property to add the owner to.</param>
 /// <param name="ownerType">The type of the class that registers the property.</param>
 protected StyledPropertyBase(StyledPropertyBase <TValue> source, Type ownerType)
     : base(source, ownerType, null)
 {
     _inherits = source.Inherits;
 }
コード例 #15
0
ファイル: AvaloniaObject.cs プロジェクト: josegomez/Avalonia
 /// <summary>
 /// Coerces the specified <see cref="AvaloniaProperty"/>.
 /// </summary>
 /// <typeparam name="T">The type of the property.</typeparam>
 /// <param name="property">The property.</param>
 public void CoerceValue <T>(StyledPropertyBase <T> property)
 {
     _values?.CoerceValue(property);
 }
コード例 #16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StyledPropertyBase{T}"/> class.
 /// </summary>
 /// <param name="source">The property to add the owner to.</param>
 /// <param name="ownerType">The type of the class that registers the property.</param>
 internal StyledProperty(StyledPropertyBase <TValue> source, Type ownerType)
     : base(source, ownerType)
 {
 }