示例#1
0
        public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            switch (values.Length)
            {
            case 1:
                var uniform = ConverterHelper.ConvertToDouble(values[0], culture);
                return(new Thickness(uniform));

            case 2:
                var horizontal = ConverterHelper.ConvertToDouble(values[0], culture);
                var vertical   = ConverterHelper.ConvertToDouble(values[1], culture);
                return(new Thickness(horizontal, vertical, horizontal, vertical));

            case 4:
                var left   = ConverterHelper.ConvertToDouble(values[0], culture);
                var top    = ConverterHelper.ConvertToDouble(values[1], culture);
                var right  = ConverterHelper.ConvertToDouble(values[2], culture);
                var bottom = ConverterHelper.ConvertToDouble(values[3], culture);
                return(new Thickness(left, top, right, bottom));

            default:
                throw new ArgumentException($"Inconsistent number of parameters: expected 1, 2 or 4 values, got {values.Length}.", nameof(values));
            }
        }
示例#2
0
        /// <inheritdoc/>
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var doubleValue     = ConverterHelper.ConvertToDouble(value, culture);
            var doubleParameter = ConverterHelper.ConvertToDouble(parameter, culture);

            return(System.Convert.ChangeType(Math.Max(doubleValue, doubleParameter), value?.GetType() ?? targetType));
        }
示例#3
0
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var doubleValue     = ConverterHelper.ConvertToDouble(value, culture);
            var doubleParameter = ConverterHelper.ConvertToDouble(parameter, culture);

            return(Compare(doubleValue, doubleParameter));
        }
示例#4
0
        /// <inheritdoc/>
        public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double scalar;

            try
            {
                scalar = ConverterHelper.ConvertToDouble(value, culture);
            }
            catch (Exception exception)
            {
                throw new ArgumentException("The value of this converter must be convertible to a double.", exception);
            }

            double param;

            try
            {
                param = ConverterHelper.ConvertToDouble(parameter, culture);
            }
            catch (Exception exception)
            {
                throw new ArgumentException("The parameter of this converter must be convertible to a double.", exception);
            }

            if (Math.Abs(param) > double.Epsilon)
            {
                return(System.Convert.ChangeType(scalar / param, targetType));
            }

            return(DependencyProperty.UnsetValue);
        }
示例#5
0
        /// <inheritdoc/>
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double scalar;

            try
            {
                scalar = ConverterHelper.ConvertToDouble(value, culture);
            }
            catch (Exception exception)
            {
                throw new ArgumentException("The value of this converter must be convertible to a double.", exception);
            }

            double param;

            try
            {
                param = ConverterHelper.ConvertToDouble(parameter, culture);
            }
            catch (Exception exception)
            {
                throw new ArgumentException("The parameter of this converter must be convertible to a double.", exception);
            }

            return(System.Convert.ChangeType(scalar * param, targetType));
        }
示例#6
0
        /// <inheritdoc/>
        public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var doubleValue     = ConverterHelper.ConvertToDouble(value, culture);
            var doubleParameter = ConverterHelper.ConvertToDouble(parameter, culture);
            var result          = doubleValue - doubleParameter;

            return(System.Convert.ChangeType(result, targetType));
        }
示例#7
0
        public override object Convert([NotNull] object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Length < 2)
            {
                throw new InvalidOperationException("This multi converter must be invoked with at least two elements");
            }

            var result = 1.0;

            try
            {
                result = values.Select(x => ConverterHelper.ConvertToDouble(x, culture)).Aggregate(result, (current, next) => current * next);
            }
            catch (Exception exception)
            {
                throw new ArgumentException("The values of this converter must be convertible to a double.", exception);
            }

            return(result);
        }
示例#8
0
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double scalar;

            try
            {
                scalar = ConverterHelper.ConvertToDouble(value, culture);
            }
            catch (Exception exception)
            {
                throw new ArgumentException("The value of this converter must be convertible to a double.", exception);
            }

            if (!(parameter is Size))
            {
                throw new ArgumentException("The parameter of this converter must be an instance of the Size structure. Use {sskk:Size (arguments)} to construct one.");
            }

            var size   = (Size)parameter;
            var result = new Size(size.Width * scalar, size.Height * scalar);

            return(result);
        }
示例#9
0
        /// <inheritdoc/>
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double scalar;

            try
            {
                scalar = ConverterHelper.ConvertToDouble(value, culture);
            }
            catch (Exception exception)
            {
                throw new ArgumentException("The value of this converter must be convertible to a double.", exception);
            }

            if (!(parameter is Thickness))
            {
                throw new ArgumentException("The parameter of this converter must be an instance of the Thickness structure. Use {sskk:Thickness (arguments)} to construct one.");
            }

            var thickness = (Thickness)parameter;
            var result    = new Thickness(thickness.Left * scalar, thickness.Top * scalar, thickness.Right * scalar, thickness.Bottom * scalar);

            return(result);
        }
示例#10
0
        public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var seconds = ConverterHelper.ConvertToDouble(value, culture);

            return(TimeSpan.FromSeconds(seconds));
        }