/// <summary> /// Sets the value of a direct property. /// </summary> /// <param name="property">The property.</param> /// <param name="value">The value.</param> private void SetDirectValue(AvaloniaProperty property, object value) { var notification = value as BindingNotification; if (notification != null) { notification.LogIfError(this, property); value = notification.Value; } if (notification == null || notification.ErrorType == BindingErrorType.Error || notification.HasValue) { var metadata = (IDirectPropertyMetadata)property.GetMetadata(GetType()); var accessor = (IDirectPropertyAccessor)GetRegistered(property); var finalValue = value == AvaloniaProperty.UnsetValue ? metadata.UnsetValue : value; LogPropertySet(property, value, BindingPriority.LocalValue); accessor.SetValue(this, finalValue); } if (notification != null) { UpdateDataValidation(property, notification); } }
/// <summary> /// Binds a property on an <see cref="IAvaloniaObject"/> to an <see cref="IBinding"/>. /// </summary> /// <param name="target">The object.</param> /// <param name="property">The property to bind.</param> /// <param name="binding">The binding.</param> /// <param name="anchor"> /// An optional anchor from which to locate required context. When binding to objects that /// are not in the logical tree, certain types of binding need an anchor into the tree in /// order to locate named controls or resources. The <paramref name="anchor"/> parameter /// can be used to provice this context. /// </param> /// <returns>An <see cref="IDisposable"/> which can be used to cancel the binding.</returns> public static IDisposable Bind( this IAvaloniaObject target, AvaloniaProperty property, IBinding binding, object anchor = null) { Contract.Requires <ArgumentNullException>(target != null); Contract.Requires <ArgumentNullException>(property != null); Contract.Requires <ArgumentNullException>(binding != null); var metadata = property.GetMetadata(target.GetType()) as IDirectPropertyMetadata; var result = binding.Initiate( target, property, anchor, metadata?.EnableDataValidation ?? false); if (result != null) { return(BindingOperations.Apply(target, property, result, anchor)); } else { return(Disposable.Empty); } }
/// <summary> /// Sets the value of a direct property. /// </summary> /// <param name="property">The property.</param> /// <param name="value">The value.</param> private void SetDirectValue(AvaloniaProperty property, object value) { var notification = value as BindingNotification; if (notification != null) { if (notification.ErrorType == BindingErrorType.Error) { Logger.Error( LogArea.Binding, this, "Error in binding to {Target}.{Property}: {Message}", this, property, ExceptionUtilities.GetMessage(notification.Error)); } if (notification.HasValue) { value = notification.Value; } } if (notification == null || notification.HasValue) { var metadata = (IDirectPropertyMetadata)property.GetMetadata(GetType()); var accessor = (IDirectPropertyAccessor)GetRegistered(property); var finalValue = value == AvaloniaProperty.UnsetValue ? metadata.UnsetValue : value; LogPropertySet(property, value, BindingPriority.LocalValue); accessor.SetValue(this, finalValue); } if (notification != null) { UpdateDataValidation(property, notification); } }
/// <summary> /// Converts an unset value to the default value for a direct property. /// </summary> /// <param name="value">The value.</param> /// <param name="property">The property.</param> /// <returns>The value.</returns> private object DirectUnsetToDefault(object value, AvaloniaProperty property) { return(value == AvaloniaProperty.UnsetValue ? ((IDirectPropertyMetadata)property.GetMetadata(GetType())).UnsetValue : value); }