/// <summary> /// Initializes a new instance of the <see cref="InstancedBinding"/> class. /// </summary> /// <param name="value"> /// The value used for the <see cref="BindingMode.OneTime"/> binding. /// </param> /// <param name="priority">The binding priority.</param> public InstancedBinding(object value, BindingPriority priority = BindingPriority.LocalValue) { Mode = BindingMode.OneTime; Priority = priority; Value = value; }
public bool SetTargetValue(object value, BindingPriority priority) { if (_accessor != null) { try { return _accessor.SetValue(value, priority); } catch { } } return false; }
public override bool SetValue(object value, BindingPriority priority) { if (!_property.IsReadOnly) { Instance.SetValue(_property, value, priority); return true; } return false; }
/// <summary> /// Initializes a new instance of the <see cref="PerspexPropertyValue"/> class. /// </summary> /// <param name="property">The property.</param> /// <param name="value">The current property value.</param> /// <param name="priority">The priority of the current value.</param> /// <param name="diagnostic">A diagnostic string.</param> public PerspexPropertyValue( PerspexProperty property, object value, BindingPriority priority, string diagnostic) { this.Property = property; this.Value = value; this.Priority = priority; this.Diagnostic = diagnostic; }
/// <summary> /// Initializes a new instance of the <see cref="InstancedBinding"/> class. /// </summary> /// <param name="subject">The subject for a two-way binding.</param> /// <param name="mode">The binding mode.</param> /// <param name="priority">The binding priority.</param> public InstancedBinding( ISubject<object> subject, BindingMode mode = BindingMode.OneWay, BindingPriority priority = BindingPriority.LocalValue) { Contract.Requires<ArgumentNullException>(subject != null); Mode = mode; Priority = priority; Subject = subject; }
/// <summary> /// Initializes a new instance of the <see cref="AvaloniaPropertyValue"/> class. /// </summary> /// <param name="property">The property.</param> /// <param name="value">The current property value.</param> /// <param name="priority">The priority of the current value.</param> /// <param name="diagnostic">A diagnostic string.</param> public AvaloniaPropertyValue( AvaloniaProperty property, object value, BindingPriority priority, string diagnostic) { Property = property; Value = value; Priority = priority; Diagnostic = diagnostic; }
/// <summary> /// Initializes a new instance of the <see cref="AvaloniaPropertyChangedEventArgs"/> class. /// </summary> /// <param name="sender">The object that the property changed on.</param> /// <param name="property">The property that changed.</param> /// <param name="oldValue">The old value of the property.</param> /// <param name="newValue">The new value of the property.</param> /// <param name="priority">The priority of the binding that produced the value.</param> public AvaloniaPropertyChangedEventArgs( AvaloniaObject sender, AvaloniaProperty property, object oldValue, object newValue, BindingPriority priority) { Sender = sender; Property = property; OldValue = oldValue; NewValue = newValue; Priority = priority; }
/// <summary> /// Initializes a new instance of the <see cref="PerspexPropertyChangedEventArgs"/> class. /// </summary> /// <param name="sender">The object that the property changed on.</param> /// <param name="property">The property that changed.</param> /// <param name="oldValue">The old value of the property.</param> /// <param name="newValue">The new value of the property.</param> /// <param name="priority">The priority of the binding that produced the value.</param> public PerspexPropertyChangedEventArgs( PerspexObject sender, PerspexProperty property, object oldValue, object newValue, BindingPriority priority) { Sender = sender; Property = property; OldValue = oldValue; NewValue = newValue; Priority = priority; }
protected override bool SetTargetValueCore(object value, BindingPriority priority) { if (_accessor != null) { try { return(_accessor.SetValue(value, priority)); } catch { } } return(false); }
public override bool SetValue(object value, BindingPriority priority) { if (_property.CanSet && _reference.TryGetTarget(out var o)) { _property.Set(o, value); SendCurrentValue(); return(true); } return(false); }
void IValueSink.ValueChanged <TValue>( StyledPropertyBase <TValue> property, BindingPriority priority, Optional <TValue> oldValue, BindingValue <TValue> newValue) { if (priority == BindingPriority.LocalValue) { _localValue = default; } UpdateEffectiveValue(); }
/// <summary> /// Initializes a new instance of the <see cref="PerspexPropertyChangedEventArgs"/> class. /// </summary> /// <param name="sender">The object that the property changed on.</param> /// <param name="property">The property that changed.</param> /// <param name="oldValue">The old value of the property.</param> /// <param name="newValue">The new value of the property.</param> /// <param name="priority">The priority of the binding that produced the value.</param> public PerspexPropertyChangedEventArgs( PerspexObject sender, PerspexProperty property, object oldValue, object newValue, BindingPriority priority) { this.Sender = sender; this.Property = property; this.OldValue = oldValue; this.NewValue = newValue; this.Priority = priority; }
public BindingEntry( IAvaloniaObject owner, StyledPropertyBase <T> property, IObservable <BindingValue <T> > source, BindingPriority priority, IValueSink sink) { _owner = owner; Property = property; Source = source; Priority = priority; _sink = sink; }
protected override void OnPropertyChanged <T>( AvaloniaProperty <T> property, Optional <T> oldValue, BindingValue <T> newValue, BindingPriority priority) { base.OnPropertyChanged(property, oldValue, newValue, priority); if (property == PositionProperty) { UpdatePseudoClasses(newValue.GetValueOrDefault <NotificationPosition>()); } }
protected override void OnPropertyChanged <T>( AvaloniaProperty <T> property, Optional <T> oldValue, BindingValue <T> newValue, BindingPriority priority) { base.OnPropertyChanged(property, oldValue, newValue, priority); if (property == SelectedDateProperty) { DataValidationErrors.SetError(this, newValue.Error); } }
/// <summary> /// Binds a <see cref="AvaloniaProperty"/> to an observable. /// </summary> /// <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( AvaloniaProperty property, IObservable <object> source, BindingPriority priority = BindingPriority.LocalValue) { Contract.Requires <ArgumentNullException>(property != null); Contract.Requires <ArgumentNullException>(source != null); VerifyAccess(); var description = GetDescription(source); if (property.IsDirect) { if (property.IsReadOnly) { throw new ArgumentException($"The property {property.Name} is readonly."); } Logger.Verbose( LogArea.Property, this, "Bound {Property} to {Binding} with priority LocalValue", property, description); if (_directBindings == null) { _directBindings = new List <DirectBindingSubscription>(); } return(new DirectBindingSubscription(this, property, source)); } else { Logger.Verbose( LogArea.Property, this, "Bound {Property} to {Binding} with priority {Priority}", property, description, priority); if (_values == null) { _values = new ValueStore(this); } return(_values.AddBinding(property, source, priority)); } }
/// <summary> /// Sets the value of a styled property. /// </summary> /// <param name="property">The property.</param> /// <param name="value">The value.</param> /// <param name="priority">The priority of the value.</param> private void SetStyledValue(AvaloniaProperty property, object value, BindingPriority priority) { var notification = value as BindingNotification; // We currently accept BindingNotifications for non-direct properties but we just // strip them to their underlying value. if (notification != null) { if (!notification.HasValue) { return; } else { value = notification.Value; } } var originalValue = value; if (!AvaloniaPropertyRegistry.Instance.IsRegistered(this, property)) { ThrowNotRegistered(property); } if (!TypeUtilities.TryConvertImplicit(property.PropertyType, value, out value)) { throw new ArgumentException(string.Format( "Invalid value for Property '{0}': '{1}' ({2})", property.Name, originalValue, originalValue?.GetType().FullName ?? "(null)")); } PriorityValue v; if (!_values.TryGetValue(property, out v)) { if (value == AvaloniaProperty.UnsetValue) { return; } v = CreatePriorityValue(property); _values.Add(property, v); } LogPropertySet(property, value, priority); v.SetValue(value, (int)priority); }
public void SetValue(T value, BindingPriority priority) { if (priority == BindingPriority.LocalValue) { _localValue = value; } else { var insert = FindInsertPoint(priority); _entries.Insert(insert, new ConstantValueEntry <T>(Property, value, priority)); } UpdateEffectiveValue(); }
protected override bool SetTargetValueCore(object?value, BindingPriority priority) { try { Target.TryGetTarget(out var target); _setDelegate.DynamicInvoke(target, value); return(true); } catch (Exception) { return(false); } }
/// <summary> /// Binds a <see cref="PerspexProperty"/> to an observable. /// </summary> /// <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( PerspexProperty property, IObservable <object> source, BindingPriority priority = BindingPriority.LocalValue) { Contract.Requires <ArgumentNullException>(property != null); VerifyAccess(); if (property.IsDirect) { property = GetRegistered(property); if (property.Setter == null) { throw new ArgumentException($"The property {property.Name} is readonly."); } _propertyLog.Verbose( "Bound {Property} to {Binding} with priority LocalValue", property, GetDescription(source)); return(source .Select(x => TypeUtilities.CastOrDefault(x, property.PropertyType)) .Subscribe(x => SetValue(property, x))); } else { PriorityValue v; if (!PerspexPropertyRegistry.Instance.IsRegistered(this, property)) { ThrowNotRegistered(property); } if (!_values.TryGetValue(property, out v)) { v = CreatePriorityValue(property); _values.Add(property, v); } _propertyLog.Verbose( "Bound {Property} to {Binding} with priority {Priority}", property, GetDescription(source), priority); return(v.Add(source, (int)priority)); } }
private void UpdateEffectiveValue() { var reachedLocalValues = false; var value = default(Optional <T>); if (_entries.Count > 0) { for (var i = _entries.Count - 1; i >= 0; --i) { var entry = _entries[i]; if (!reachedLocalValues && entry.Priority >= BindingPriority.LocalValue) { reachedLocalValues = true; if (_localValue.HasValue) { value = _localValue; ValuePriority = BindingPriority.LocalValue; break; } } if (entry.Value.HasValue) { value = entry.Value; ValuePriority = entry.Priority; break; } } } else if (_localValue.HasValue) { value = _localValue; ValuePriority = BindingPriority.LocalValue; } if (value.HasValue && _coerceValue != null) { value = _coerceValue(_owner, value.Value); } if (value != Value) { var old = Value; Value = value; _sink.ValueChanged(Property, ValuePriority, old, value); } }
private int FindInsertPoint(BindingPriority priority) { var result = _entries.Count; for (var i = 0; i < _entries.Count; ++i) { if (_entries[i].Priority < priority) { result = i; break; } } return(result); }
/// <summary> /// Gets a subject for a <see cref="AvaloniaProperty"/>. /// </summary> /// <typeparam name="T">The property type.</typeparam> /// <param name="o">The object.</param> /// <param name="property">The property.</param> /// <param name="priority"> /// The priority with which binding values are written to the object. /// </param> /// <returns> /// An <see cref="ISubject{T}"/> which can be used for two-way binding to/from the /// property. /// </returns> public static ISubject <BindingValue <T> > GetBindingSubject <T>( this IAvaloniaObject o, AvaloniaProperty <T> property, BindingPriority priority = BindingPriority.LocalValue) { return(Subject.Create <BindingValue <T> >( Observer.Create <BindingValue <T> >(x => { if (x.HasValue) { o.SetValue(property, x.Value, priority); } }), o.GetBindingObservable(property))); }
/// <summary> /// Sets a <see cref="PerspexProperty"/> value. /// </summary> /// <param name="property">The property.</param> /// <param name="value">The value.</param> /// <param name="priority">The priority of the value.</param> public void SetValue( PerspexProperty property, object value, BindingPriority priority = BindingPriority.LocalValue) { Contract.Requires <ArgumentNullException>(property != null); VerifyAccess(); if (property.IsDirect) { var accessor = (IDirectPropertyAccessor)GetRegistered(property); LogPropertySet(property, value, priority); accessor.SetValue(this, DirectUnsetToDefault(value, property)); } else { PriorityValue v; var originalValue = value; if (!PerspexPropertyRegistry.Instance.IsRegistered(this, property)) { ThrowNotRegistered(property); } if (!TypeUtilities.TryCast(property.PropertyType, value, out value)) { throw new ArgumentException(string.Format( "Invalid value for Property '{0}': '{1}' ({2})", property.Name, originalValue, originalValue?.GetType().FullName ?? "(null)")); } if (!_values.TryGetValue(property, out v)) { if (value == PerspexProperty.UnsetValue) { return; } v = CreatePriorityValue(property); _values.Add(property, v); } LogPropertySet(property, value, priority); v.SetValue(value, (int)priority); } }
/// <summary> /// Initiates a two-way binding between a <see cref="PerspexProperty"/> and an /// <see cref="ISubject{Object}"/>. /// </summary> /// <param name="property">The property on this object.</param> /// <param name="source">The subject to bind to.</param> /// <param name="priority">The priority of the binding.</param> /// <returns> /// A disposable which can be used to terminate the binding. /// </returns> /// <remarks> /// The binding is first carried out from <paramref name="source"/> to this. /// </remarks> public IDisposable BindTwoWay( PerspexProperty property, ISubject <object> source, BindingPriority priority = BindingPriority.LocalValue) { VerifyAccess(); _propertyLog.Verbose( "Bound two way {Property} to {Binding} with priority {Priority}", property, GetDescription(source), priority); return(new CompositeDisposable( Bind(property, source), GetObservable(property).Subscribe(source))); }
protected override bool SetTargetValueCore(object?value, BindingPriority priority) { try { if (Target.TryGetTarget(out var target) && target is IAvaloniaObject obj) { obj.SetValue(_property, value, priority); return(true); } return(false); } catch { return(false); } }
/// <summary> /// Attempts to set the value of a property expression. /// </summary> /// <param name="value">The value to set.</param> /// <param name="priority">The binding priority to use.</param> /// <returns> /// True if the value could be set; false if the expression does not evaluate to a /// property. Note that the <see cref="ExpressionObserver"/> must be subscribed to /// before setting the target value can work, as setting the value requires the /// expression to be evaluated. /// </returns> public bool SetValue(object?value, BindingPriority priority = BindingPriority.LocalValue) { if (Leaf is SettableNode settable) { foreach (var transform in TransformNodes) { value = transform.Transform(value); if (value is BindingNotification) { return(false); } } return(settable.SetTargetValue(value, priority)); } return(false); }
/// <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."), });
public override bool SetValue(object value, BindingPriority priority) { if (Next != null) { return Next.SetValue(value, priority); } else { if (_accessor != null) { return _accessor.SetValue(value, priority); } return false; } }
public override bool SetValue(object value, BindingPriority priority) { if (Next != null) { return(Next.SetValue(value, priority)); } else { if (_accessor != null) { return(_accessor.SetValue(value, priority)); } return(false); } }
/// <summary> /// Initiates a two-way binding between <see cref="PerspexProperty"/>s. /// </summary> /// <param name="property">The property on this object.</param> /// <param name="source">The source object.</param> /// <param name="sourceProperty">The property on the source object.</param> /// <param name="priority">The priority of the binding.</param> /// <returns> /// A disposable which can be used to terminate the binding. /// </returns> /// <remarks> /// The binding is first carried out from <paramref name="source"/> to this. /// </remarks> public IDisposable BindTwoWay( PerspexProperty property, PerspexObject source, PerspexProperty sourceProperty, BindingPriority priority = BindingPriority.LocalValue) { VerifyAccess(); _propertyLog.Verbose( "Bound two way {Property} to {Binding} with priority {Priority}", property, source, priority); return(new CompositeDisposable( Bind(property, source.GetObservable(sourceProperty)), source.Bind(sourceProperty, GetObservable(property)))); }
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 !;
public override bool SetValue(object value, BindingPriority priority) { try { return base.SetValue(value, priority); } catch (TargetInvocationException ex) { Observer.OnNext(new BindingNotification(ex.InnerException, BindingErrorType.DataValidationError)); } catch (Exception ex) { Observer.OnNext(new BindingNotification(ex, BindingErrorType.DataValidationError)); } return false; }
public override bool SetValue(object value, BindingPriority priority) { if (_property.CanWrite) { _eventRaised = false; _property.SetValue(_reference.Target, value); if (!_eventRaised) { SendCurrentValue(); } return(true); } return(false); }
/// <summary> /// Sets a <see cref="AvaloniaProperty"/> value. /// </summary> /// <param name="property">The property.</param> /// <param name="value">The value.</param> /// <param name="priority">The priority of the value.</param> public void SetValue( AvaloniaProperty property, object value, BindingPriority priority = BindingPriority.LocalValue) { Contract.Requires <ArgumentNullException>(property != null); VerifyAccess(); if (property.IsDirect) { SetDirectValue(property, value); } else { SetStyledValue(property, value, priority); } }
public override bool SetValue(object?value, BindingPriority priority) { try { return(base.SetValue(value, priority)); } catch (TargetInvocationException ex) when(ex.InnerException is not null) { PublishValue(new BindingNotification(ex.InnerException, BindingErrorType.DataValidationError)); } catch (Exception ex) { PublishValue(new BindingNotification(ex, BindingErrorType.DataValidationError)); } return(false); }
public override bool SetValue(object value, BindingPriority priority) { try { return(base.SetValue(value, priority)); } catch (TargetInvocationException ex) { Observer.OnNext(new BindingNotification(ex.InnerException, BindingErrorType.DataValidationError)); } catch (Exception ex) { Observer.OnNext(new BindingNotification(ex, BindingErrorType.DataValidationError)); } return(false); }
/// <summary> /// Raises the <see cref="PropertyChanged"/> event. /// </summary> /// <param name="property">The property that has changed.</param> /// <param name="oldValue">The old property value.</param> /// <param name="newValue">The new property value.</param> /// <param name="priority">The priority of the binding that produced the value.</param> protected void RaisePropertyChanged( PerspexProperty property, object oldValue, object newValue, BindingPriority priority) { Contract.Requires <ArgumentNullException>(property != null); VerifyAccess(); PerspexPropertyChangedEventArgs e = new PerspexPropertyChangedEventArgs( this, property, oldValue, newValue, priority); if (property.Notifying != null) { property.Notifying(this, true); } try { OnPropertyChanged(e); property.NotifyChanged(e); if (PropertyChanged != null) { PropertyChanged(this, e); } if (_inpcChanged != null) { PropertyChangedEventArgs e2 = new PropertyChangedEventArgs(property.Name); _inpcChanged(this, e2); } } finally { if (property.Notifying != null) { property.Notifying(this, false); } } }
/// <summary> /// Gets a subject for a <see cref="AvaloniaProperty"/>. /// </summary> /// <typeparam name="T">The property type.</typeparam> /// <param name="o">The object.</param> /// <param name="property">The property.</param> /// <param name="priority"> /// The priority with which binding values are written to the object. /// </param> /// <returns> /// An <see cref="ISubject{T}"/> which can be used for two-way binding to/from the /// property. /// </returns> public static ISubject <T> GetSubject <T>( this IAvaloniaObject o, AvaloniaProperty <T> property, BindingPriority priority = BindingPriority.LocalValue) { // TODO: Subject.Create<T> is not yet in stable Rx : once it is, remove the // AnonymousSubject classes from this file and use Subject.Create<T>. var output = new Subject <T>(); var result = new AnonymousSubject <T>( Observer.Create <T>( x => output.OnNext(x), e => output.OnError(e), () => output.OnCompleted()), o.GetObservable(property)); o.Bind(property, output, priority); return(result); }
public override bool SetValue(object value, BindingPriority priority) { try { var success = base.SetValue(value, priority); SendValidationCallback(new ExceptionValidationStatus(null)); return success; } catch (TargetInvocationException ex) { SendValidationCallback(new ExceptionValidationStatus(ex.InnerException)); } catch (Exception ex) { SendValidationCallback(new ExceptionValidationStatus(ex)); } return false; }
/// <summary> /// Initializes a new instance of the <see cref="InstancedBinding"/> class. /// </summary> /// <param name="observable">The observable for a one-way binding.</param> /// <param name="mode">The binding mode.</param> /// <param name="priority">The binding priority.</param> public InstancedBinding( IObservable<object> observable, BindingMode mode = BindingMode.OneWay, BindingPriority priority = BindingPriority.LocalValue) { Contract.Requires<ArgumentNullException>(observable != null); if (mode == BindingMode.OneWayToSource || mode == BindingMode.TwoWay) { throw new ArgumentException( "Invalid BindingResult mode: OneWayToSource and TwoWay bindings" + "require a Subject."); } Mode = mode; Priority = priority; Observable = observable; }
/// <inheritdoc/> public bool SetValue(object value, BindingPriority priority) { return false; }
public override bool SetValue(object value, BindingPriority priority) { return base.SetValue(value, priority); }
public void SetValue(PerspexProperty property, object value, BindingPriority priority) { throw new NotImplementedException(); }
public IDisposable Bind(PerspexProperty property, IObservable<object> source, BindingPriority priority) { throw new NotImplementedException(); }
/// <inheritdoc/> public virtual bool SetValue(object value, BindingPriority priority) => _accessor.SetValue(value, priority);
/// <inheritdoc/> public abstract bool SetValue(object value, BindingPriority priority);