/// <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); } }
/// <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)); }
/// <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) { }
/// <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()}")); }
/// <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); }
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)); }
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())); }
/// <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; }
public DirectBindingSubscription( AvaloniaObject owner, DirectPropertyBase <T> property, IObservable <BindingValue <T> > source) { _owner = owner; _property = property; _owner._directBindings.Add(this); _subscription = source.Subscribe(this); }
/// <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; }
/// <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); } }
/// <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."), });
/// <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); }
/// <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); }