コード例 #1
0
ファイル: AvaloniaObject.cs プロジェクト: josegomez/Avalonia
        /// <summary>
        /// Sets the value of a direct property.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="value">The value.</param>
        private void SetDirectValueUnchecked <T>(DirectPropertyBase <T> property, BindingValue <T> value)
        {
            var p = AvaloniaPropertyRegistry.Instance.FindRegisteredDirect(this, property);

            if (p == null)
            {
                throw new ArgumentException($"Property '{property.Name} not registered on '{this.GetType()}");
            }

            LogIfError(property, value);

            switch (value.Type)
            {
            case BindingValueType.UnsetValue:
            case BindingValueType.BindingError:
                var fallback = value.HasValue ? value : value.WithValue(property.GetUnsetValue(GetType()));
                property.InvokeSetter(this, fallback);
                break;

            case BindingValueType.DataValidationError:
                property.InvokeSetter(this, value);
                break;

            case BindingValueType.Value:
            case BindingValueType.BindingErrorWithFallback:
            case BindingValueType.DataValidationErrorWithFallback:
                property.InvokeSetter(this, value);
                break;
            }

            if (p.IsDataValidationEnabled)
            {
                UpdateDataValidation(property, value);
            }
        }
コード例 #2
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>
        /// <returns>
        /// A disposable which can be used to terminate the binding.
        /// </returns>
        public IDisposable Bind <T>(
            DirectPropertyBase <T> property,
            IObservable <BindingValue <T> > source)
        {
            property = property ?? throw new ArgumentNullException(nameof(property));
            source   = source ?? throw new ArgumentNullException(nameof(source));
            VerifyAccess();

            property = AvaloniaPropertyRegistry.Instance.GetRegisteredDirect(this, property);

            if (property.IsReadOnly)
            {
                throw new ArgumentException($"The property {property.Name} is readonly.");
            }

            Logger.TryGet(LogEventLevel.Verbose)?.Log(
                LogArea.Property,
                this,
                "Bound {Property} to {Binding} with priority LocalValue",
                property,
                GetDescription(source));

            _directBindings ??= new List <IDisposable>();

            return(new DirectBindingSubscription <T>(this, property, source));
        }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DirectPropertyBase{TValue}"/> class.
 /// </summary>
 /// <param name="source">The property to copy.</param>
 /// <param name="ownerType">The new owner type.</param>
 /// <param name="metadata">Optional overridden metadata.</param>
 protected DirectPropertyBase(
     DirectPropertyBase <TValue> source,
     Type ownerType,
     AvaloniaPropertyMetadata metadata)
     : base(source, ownerType, metadata)
 {
 }
コード例 #4
0
 /// <summary>
 /// Gets a direct property as registered on an object.
 /// </summary>
 /// <param name="o">The object.</param>
 /// <param name="property">The direct property.</param>
 /// <returns>
 /// The registered.
 /// </returns>
 public DirectPropertyBase <T> GetRegisteredDirect <T>(
     IAvaloniaObject o,
     DirectPropertyBase <T> property)
 {
     return(FindRegisteredDirect(o, property) ??
            throw new ArgumentException($"Property '{property.Name} not registered on '{o.GetType()}"));
 }
コード例 #5
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>
        public void SetValue <T>(DirectPropertyBase <T> property, T value)
        {
            property = property ?? throw new ArgumentNullException(nameof(property));
            VerifyAccess();

            LogPropertySet(property, value, BindingPriority.LocalValue);
            SetDirectValueUnchecked(property, value);
        }
コード例 #6
0
        public T GetValue <T>(DirectPropertyBase <T> property)
        {
            property = property ?? throw new ArgumentNullException(nameof(property));
            VerifyAccess();

            var registered = AvaloniaPropertyRegistry.Instance.GetRegisteredDirect(this, property);

            return(registered.InvokeGetter(this));
        }
コード例 #7
0
        public void ClearValue <T>(DirectPropertyBase <T> property)
        {
            property = property ?? throw new ArgumentNullException(nameof(property));
            VerifyAccess();

            var p = AvaloniaPropertyRegistry.Instance.GetRegisteredDirect(this, property);

            p.InvokeSetter(this, p.GetUnsetValue(GetType()));
        }
コード例 #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AvaloniaProperty"/> class.
 /// </summary>
 /// <param name="source">The property to copy.</param>
 /// <param name="getter">Gets the current value of the property.</param>
 /// <param name="setter">Sets the value of the property. May be null.</param>
 /// <param name="metadata">Optional overridden metadata.</param>
 private DirectProperty(
     DirectPropertyBase <TValue> source,
     Func <TOwner, TValue> getter,
     Action <TOwner, TValue>?setter,
     DirectPropertyMetadata <TValue> metadata)
     : base(source, typeof(TOwner), metadata)
 {
     Getter = getter ?? throw new ArgumentNullException(nameof(getter));
     Setter = setter;
 }
コード例 #9
0
ファイル: AvaloniaObject.cs プロジェクト: josegomez/Avalonia
 public DirectBindingSubscription(
     AvaloniaObject owner,
     DirectPropertyBase <T> property,
     IObservable <BindingValue <T> > source)
 {
     _owner    = owner;
     _property = property;
     _owner._directBindings.Add(this);
     _subscription = source.Subscribe(this);
 }
コード例 #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AvaloniaProperty"/> class.
        /// </summary>
        /// <param name="source">The property to copy.</param>
        /// <param name="getter">Gets the current value of the property.</param>
        /// <param name="setter">Sets the value of the property. May be null.</param>
        /// <param name="metadata">Optional overridden metadata.</param>
        private DirectProperty(
            DirectPropertyBase <TValue> source,
            Func <TOwner, TValue> getter,
            Action <TOwner, TValue> setter,
            DirectPropertyMetadata <TValue> metadata)
            : base(source, typeof(TOwner), metadata)
        {
            Contract.Requires <ArgumentNullException>(getter != null);

            Getter = getter;
            Setter = setter;
        }
コード例 #11
0
ファイル: AvaloniaObject.cs プロジェクト: josegomez/Avalonia
        /// <summary>
        /// Sets the value of a direct property.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="value">The value.</param>
        private void SetDirectValueUnchecked <T>(DirectPropertyBase <T> property, T value)
        {
            var p = AvaloniaPropertyRegistry.Instance.GetRegisteredDirect(this, property);

            if (value is UnsetValueType)
            {
                p.InvokeSetter(this, p.GetUnsetValue(GetType()));
            }
            else if (!(value is DoNothingType))
            {
                p.InvokeSetter(this, value);
            }
        }
コード例 #12
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."),
            });
コード例 #13
0
        /// <summary>
        /// Finds a direct property as registered on an object.
        /// </summary>
        /// <param name="o">The object.</param>
        /// <param name="property">The direct property.</param>
        /// <returns>
        /// The registered property or null if no matching property found.
        /// </returns>
        public DirectPropertyBase <T> FindRegisteredDirect <T>(
            IAvaloniaObject o,
            DirectPropertyBase <T> property)
        {
            if (property.Owner == o.GetType())
            {
                return(property);
            }

            foreach (var p in GetRegisteredDirect(o.GetType()))
            {
                if (p == property)
                {
                    return((DirectPropertyBase <T>)p);
                }
            }

            return(null);
        }
コード例 #14
0
        /// <summary>
        /// Finds a direct property as registered on an object.
        /// </summary>
        /// <param name="o">The object.</param>
        /// <param name="property">The direct property.</param>
        /// <returns>
        /// The registered property or null if no matching property found.
        /// </returns>
        public DirectPropertyBase <T>?FindRegisteredDirect <T>(
            IAvaloniaObject o,
            DirectPropertyBase <T> property)
        {
            if (property.Owner == o.GetType())
            {
                return(property);
            }

            var registeredDirect      = GetRegisteredDirect(o.GetType());
            var registeredDirectCount = registeredDirect.Count;

            for (var i = 0; i < registeredDirectCount; i++)
            {
                var p = registeredDirect[i];

                if (p == property)
                {
                    return((DirectPropertyBase <T>)p);
                }
            }

            return(null);
        }