public void ConvertFrom()
        {
            SizeConverter r = new SizeConverter();

            object or = r.ConvertFrom("3, 4");

            Assert.AreEqual(typeof(Size), or.GetType());
            Assert.AreEqual(new Size(3, 4), or);
        }
        /// <inheritdoc />
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var desiredSize = Size.Empty;

            // TargetVisual can be at index 1 or index 2, depending on the number of values passed
            var targetVisual = values.Length == 2
                                   ? values[1] as Visual
                                   : null;

            if (targetVisual == null)
            {
                targetVisual = values.Length == 3
                                   ? values[2] as Visual
                                   : null;
            }

            if (values.Length >= 2 &&
                values[1] is Size desiredSizeFromIndex1)
            {
                desiredSize = desiredSizeFromIndex1;
            }
            else if (values.Length >= 2 &&
                     (values[1] is Visual) == false)
            {
                var    possibleDesiredSizeValue = values[1];
                object convertedValue;

                if (sizeConverter.CanConvertFrom(possibleDesiredSizeValue.GetType()) &&
                    (convertedValue = sizeConverter.ConvertFrom(possibleDesiredSizeValue)) != null)
                {
                    desiredSize = (Size)convertedValue;
                }
                else
                {
                    desiredSize = Size.Empty;
                }
            }

            //if (desiredSize.IsEmpty
            //    && targetVisual != null
            //    && targetVisual is FrameworkElement targetFrameworkElement
            //    && DoubleHelper.IsFinite(targetFrameworkElement.Width)
            //    && DoubleHelper.IsFinite(targetFrameworkElement.Height))
            //{
            //    desiredSize = new Size(targetFrameworkElement.Width, targetFrameworkElement.Height);
            //}

            return(this.Convert(values[0], targetVisual, desiredSize, targetType));
        }
        /// <inheritdoc />
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var desiredSize  = Size.Empty;
            var valuesLength = values.Length;

            // TargetVisual can be at index 1 or index 2, depending on the number of values passed
            var targetVisual = valuesLength == 2
                ? values[1] as Visual
                : null;

            if (targetVisual is null)
            {
                targetVisual = valuesLength == 3
                    ? values[2] as Visual
                    : null;
            }

            if (valuesLength >= 2 &&
                values[1] is Size desiredSizeFromIndex1)
            {
                desiredSize = desiredSizeFromIndex1;
            }
            else if (valuesLength >= 2 &&
                     (values[1] is Visual) == false)
            {
                var    possibleDesiredSizeValue = values[1];
                object?convertedValue;

                if (possibleDesiredSizeValue is not null &&
                    sizeConverter.CanConvertFrom(possibleDesiredSizeValue.GetType()) &&
                    (convertedValue = sizeConverter.ConvertFrom(possibleDesiredSizeValue)) is not null)
                {
                    desiredSize = (Size)convertedValue;
                }
                else
                {
                    desiredSize = Size.Empty;
                }
            }
Пример #4
0
 /// <summary>
 /// Convert a string representation to a Size instance.
 /// </summary>
 /// <param name="str">String representation of the Size instance.</param>
 /// <returns>Size instance created from string representation.</returns>
 public static Size StringToSize(string str)
 {
     return((Size)_sc.ConvertFrom(null, CultureInfo.InvariantCulture, str));
 }
        public void ConvertFrom_negative()
        {
            SizeConverter r = new SizeConverter();

            r.ConvertFrom("-1, -4");
        }
        public void ConvertFrom_size()
        {
            SizeConverter r = new SizeConverter();

            r.ConvertFrom(new Size(10, 20));
        }
Пример #7
0
 public static Size StringToSize(string str)
 {
     return((Size)_sc.ConvertFrom(str));
 }
Пример #8
0
 public void ConvertFromInvalidSource()
 {
     new Action(() => _converter.ConvertFrom(null)).Should().Throw <NotSupportedException>().WithMessage("*null*");
     new Action(() => _converter.ConvertFrom(new object())).Should().Throw <NotSupportedException>().WithMessage($"*{typeof(object)}*");
 }