Пример #1
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);
            }
Пример #2
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);
        }