예제 #1
0
        private object Translate(object o)
        {
            var weak = o as WeakReference;

            if (weak != null)
            {
                return(weak.Target);
            }
            else
            {
                var broken = BindingNotification.ExtractError(o) as MarkupBindingChainException;

                if (broken != null)
                {
                    // We've received notification of a broken expression due to a null value
                    // somewhere in the chain. If this null value occurs at the first node then we
                    // ignore it, as its likely that e.g. the DataContext has not yet been set up.
                    if (broken.HasNodes)
                    {
                        broken.Commit(Description);
                    }
                    else
                    {
                        o = AvaloniaProperty.UnsetValue;
                    }
                }
                return(o);
            }
        }
예제 #2
0
 private void ValueChanged(object value)
 {
     var broken = BindingNotification.ExtractError(value) as MarkupBindingChainException;
     broken?.Commit(Description);
     _value = new WeakReference<object>(value);
     PublishNext(value);
 }
예제 #3
0
 protected override void UpdateDataValidation(AvaloniaProperty property, BindingNotification status)
 {
     if (property == TextProperty)
     {
         DataValidationErrors.SetError(this, status.Error);
     }
 }
예제 #4
0
        protected virtual void NextValueChanged(object value)
        {
            var bindingBroken = BindingNotification.ExtractError(value) as MarkupBindingChainNullException;

            bindingBroken?.AddNode(Description);
            _observer.OnNext(value);
        }
예제 #5
0
 protected override void UpdateDataValidation(AvaloniaProperty property, BindingNotification status)
 {
     if (property == TextProperty)
     {
         var classes = (IPseudoClasses)Classes;
         DataValidationErrors = UnpackException(status.Error);
         classes.Set(":error", DataValidationErrors != null);
     }
 }
예제 #6
0
 /// <summary>
 /// Called when a priority level encounters an error.
 /// </summary>
 /// <param name="level">The priority level of the changed entry.</param>
 /// <param name="error">The binding error.</param>
 public void LevelError(PriorityLevel level, BindingNotification error)
 {
     Logger.Log(
         LogEventLevel.Error,
         LogArea.Binding,
         Owner,
         "Error in binding to {Target}.{Property}: {Message}",
         Owner,
         Property,
         error.Error.Message);
 }
예제 #7
0
파일: Button.cs 프로젝트: x2bool/Avalonia
 protected override void UpdateDataValidation(AvaloniaProperty property, BindingNotification status)
 {
     base.UpdateDataValidation(property, status);
     if (property == CommandProperty)
     {
         if (status?.ErrorType == BindingErrorType.Error)
         {
             IsEnabled = false;
         }
     }
 }
예제 #8
0
            public object Convert(IList <object> values, Type targetType, object parameter, CultureInfo culture)
            {
                if (BindingNotification.ExtractValue(values[0]) == AvaloniaProperty.UnsetValue)
                {
                    return(AvaloniaProperty.UnsetValue);
                }
                var condition   = (bool)BindingNotification.ExtractValue(values[0]);
                var trueResult  = (object)values[1];
                var falseResult = (object)values[2];

                return(condition ? trueResult : falseResult);
            }
        protected virtual void NextValueChanged(object?value)
        {
            if (_subscriber is null)
            {
                return;
            }

            var bindingBroken = BindingNotification.ExtractError(value) as MarkupBindingChainException;

            bindingBroken?.AddNode(Description ?? "{empty}");
            _subscriber(value);
        }
예제 #10
0
        private object Translate(object o)
        {
            if (o is WeakReference weak)
            {
                return(weak.Target);
            }
            else if (BindingNotification.ExtractError(o) is MarkupBindingChainException broken)
            {
                broken.Commit(Description);
            }

            return(o);
        }
예제 #11
0
        private static BindingNotification Merge(object a, BindingNotification b)
        {
            var an = a as BindingNotification;

            if (an != null)
            {
                Merge(an, b);
                return(an);
            }
            else
            {
                return(b);
            }
        }
 private void OnValidationError(BindingNotification notification)
 {
     if (notification.Error != null)
     {
         _editBinding.AlterValidationErrors(errors =>
         {
             errors.Clear();
             var unpackedErrors = ValidationUtil.UnpackException(notification.Error);
             if (unpackedErrors != null)
             {
                 errors.AddRange(unpackedErrors);
             }
         });
     }
 }
예제 #13
0
 protected override void UpdateDataValidation(AvaloniaProperty property, BindingNotification status)
 {
     base.UpdateDataValidation(property, status);
     if (property == CommandProperty)
     {
         if (status?.ErrorType == BindingErrorType.Error)
         {
             if (_commandCanExecute)
             {
                 _commandCanExecute = false;
                 UpdateIsEffectivelyEnabled();
             }
         }
     }
 }
예제 #14
0
        private BindingNotification ConvertValue(BindingNotification notification)
        {
            if (notification.HasValue)
            {
                var converted = ConvertValue(notification.Value);
                notification = Merge(notification, converted);
            }
            else if (_fallbackValue != AvaloniaProperty.UnsetValue)
            {
                var fallback = ConvertFallback();
                notification = Merge(notification, fallback);
            }

            return(notification);
        }
예제 #15
0
        private static BindingNotification Merge(BindingNotification a, object b)
        {
            var bn = b as BindingNotification;

            if (bn != null)
            {
                Merge(a, bn);
            }
            else
            {
                a.SetValue(b);
            }

            return(a);
        }
예제 #16
0
 /// <summary>
 /// Logs a mesage if the notification represents a binding error.
 /// </summary>
 /// <param name="property">The property being bound.</param>
 /// <param name="notification">The binding notification.</param>
 private void LogIfError(AvaloniaProperty property, BindingNotification notification)
 {
     if (notification.ErrorType == BindingErrorType.Error)
     {
         if (notification.Error is AggregateException aggregate)
         {
             foreach (var inner in aggregate.InnerExceptions)
             {
                 LogBindingError(property, inner);
             }
         }
         else
         {
             LogBindingError(property, notification.Error);
         }
     }
 }
예제 #17
0
        private static BindingNotification Merge(BindingNotification a, BindingNotification b)
        {
            if (b.HasValue)
            {
                a.SetValue(b.Value);
            }
            else
            {
                a.ClearValue();
            }

            if (b.Error != null)
            {
                a.AddError(b.Error, b.ErrorType);
            }

            return(a);
        }
예제 #18
0
        private object Translate(object o)
        {
            var weak = o as WeakReference;

            if (weak != null)
            {
                return(weak.Target);
            }
            else
            {
                var broken = BindingNotification.ExtractError(o) as MarkupBindingChainException;

                if (broken != null)
                {
                    broken.Commit(Description);
                }
                return(o);
            }
        }
예제 #19
0
 public static void LogIfError(
     this BindingNotification notification,
     object source,
     AvaloniaProperty property)
 {
     if (notification.ErrorType == BindingErrorType.Error)
     {
         if (notification.Error is AggregateException aggregate)
         {
             foreach (var inner in aggregate.InnerExceptions)
             {
                 LogError(source, property, inner);
             }
         }
         else
         {
             LogError(source, property, notification.Error);
         }
     }
 }
예제 #20
0
        private static object Negate(object?value)
        {
            var notification = value as BindingNotification;
            var v            = BindingNotification.ExtractValue(value);

            BindingNotification GenerateError(Exception e)
            {
                notification ??= new BindingNotification(AvaloniaProperty.UnsetValue);
                notification.AddError(e, BindingErrorType.Error);
                notification.ClearValue();
                return(notification);
            }

            if (v != AvaloniaProperty.UnsetValue)
            {
                var s = v as string;

                if (s != null)
                {
                    bool result;

                    if (bool.TryParse(s, out result))
                    {
                        return(!result);
                    }
                    else
                    {
                        return(GenerateError(new InvalidCastException($"Unable to convert '{s}' to bool.")));
                    }
                }
                else
                {
                    try
                    {
                        var boolean = Convert.ToBoolean(v, CultureInfo.InvariantCulture);

                        if (notification is object)
                        {
                            notification.SetValue(!boolean);
                            return(notification);
                        }
                        else
                        {
                            return(!boolean);
                        }
                    }
                    catch (InvalidCastException)
                    {
                        // The error message here is "Unable to cast object of type 'System.Object'
                        // to type 'System.IConvertible'" which is kinda useless so provide our own.
                        return(GenerateError(new InvalidCastException($"Unable to convert '{v}' to bool.")));
                    }
                    catch (Exception e)
                    {
                        return(GenerateError(e));
                    }
                }
            }

            return(notification ?? AvaloniaProperty.UnsetValue);
        }
예제 #21
0
 /// <summary>
 /// Invoked when an entry in <see cref="Bindings"/> encounters a recoverable error.
 /// </summary>
 /// <param name="entry">The entry that completed.</param>
 /// <param name="error">The error.</param>
 public void Error(PriorityBindingEntry entry, BindingNotification error)
 {
     Owner.LevelError(this, error);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="PropertyError"/> class.
 /// </summary>
 /// <param name="error">The error to report.</param>
 public PropertyError(BindingNotification error)
 {
     _error = error;
 }
예제 #23
0
 /// <summary>
 /// Called to update the validation state for properties for which data validation is
 /// enabled.
 /// </summary>
 /// <param name="property">The property.</param>
 /// <param name="status">The new validation status.</param>
 protected virtual void UpdateDataValidation(
     AvaloniaProperty property,
     BindingNotification status)
 {
 }
예제 #24
0
 internal void BindingNotificationReceived(AvaloniaProperty property, BindingNotification notification)
 {
     UpdateDataValidation(property, notification);
 }
예제 #25
0
 public void BindingNotificationReceived(AvaloniaProperty property, BindingNotification notification)
 {
     _owner.BindingNotificationReceived(property, notification);
 }
예제 #26
0
 /// <inheritdoc/>
 void IPriorityValueOwner.BindingNotificationReceived(AvaloniaProperty property, BindingNotification notification)
 {
     UpdateDataValidation(property, notification);
 }
예제 #27
0
 protected override void BindingNotificationReceived(AvaloniaProperty property, BindingNotification notification)
 {
     Notifications.Add(notification);
 }
예제 #28
0
 /// <inheritdoc/>
 void IPriorityValueOwner.BindingNotificationReceived(PriorityValue sender, BindingNotification notification)
 {
     UpdateDataValidation(sender.Property, notification);
 }
예제 #29
0
 /// <summary>
 /// Called when a priority level encounters an error.
 /// </summary>
 /// <param name="level">The priority level of the changed entry.</param>
 /// <param name="error">The binding error.</param>
 public void LevelError(PriorityLevel level, BindingNotification error)
 {
     error.LogIfError(Owner, Property);
 }
 protected override void UpdateDataValidation(AvaloniaProperty property, BindingNotification notification)
 {
     Notifications.Add(notification);
 }