public void Convert_ShouldReturnExpectedValue(object[] inputValues, Point convertedPoint, CoordinateType outputType, double expectedValue)
        {
            _fakeScatterPointToWindowsPointConverter.Convert(null, null, null, null).ReturnsForAnyArgs(convertedPoint);
            _converter.Output = CoordinateType.X;

            var actualValue = (double)_converter.Convert(inputValues, typeof(double), null, CultureInfo.CurrentCulture);

            actualValue.Should().BeApproximately(convertedPoint.X, TestUtils.APPROXIMATION_PRECISION);
        }
Пример #2
0
        /// <summary>
        /// Converts a value by executing <see cref="IMultiValueConverter.Convert"/> method of the <see cref="Converter"/>, next iterating through the <see cref="Converters"/> collection and executing <see cref="IValueConverter.Convert"/> method for each <see cref="IValueConverter"/> contained. The return value of the <see cref="IMultiValueConverter.Convert"/> method call or <see cref="IValueConverter.Convert"/> method call for current <see cref="IValueConverter"/> in the iteration is passed as input value for processing next <see cref="IValueConverter"/> in the iteration.
        /// </summary>
        /// <param name="values">The array of values produced by the source bindings in the <see cref="MultiBinding"/>. The value <see cref="DependencyProperty.UnsetValue"/> indicates that the source binding has no value to provide for conversion.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>A converted value, meaning the result of processing the chain of converters.</returns>
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            IMultiValueConverter converter = Converter;

            if (converter != null)
            {
                object value = converter.Convert(values, targetType, parameter, culture);

                if (value == Binding.DoNothing)
                {
                    return(value);
                }

                for (int i = 0; i < converters.Count; i++)
                {
                    value = converters[i].Convert(value, targetType, parameter, culture);

                    if (value == Binding.DoNothing)
                    {
                        break;
                    }
                }

                return(value);
            }

            return(Binding.DoNothing);
        }
Пример #3
0
        private object ConvertValue(IList <object> values, Type targetType, IMultiValueConverter converter)
        {
            for (var i = 0; i < values.Count; ++i)
            {
                if (values[i] is BindingNotification notification)
                {
                    values[i] = notification.Value;
                }
            }

            var culture   = CultureInfo.CurrentCulture;
            var converted = converter.Convert(values, targetType, ConverterParameter, culture);

            if (converted == null)
            {
                converted = TargetNullValue;
            }

            if (converted == AvaloniaProperty.UnsetValue)
            {
                converted = FallbackValue;
            }

            return(converted);
        }
Пример #4
0
        private object ConvertValue(IList <object> values, Type targetType, IMultiValueConverter converter)
        {
            var culture   = CultureInfo.CurrentCulture;
            var converted = converter.Convert(values, targetType, ConverterParameter, culture);

            if (converted == AvaloniaProperty.UnsetValue && FallbackValue != null)
            {
                converted = FallbackValue;
            }

            return(converted);
        }
        /// <summary>
        /// Will be called to evaluate our source value based on all available
        /// property and context states.
        /// This method will also be automatically re-called when any object involved in the
        /// evaluation process of our source value was changed.
        /// </summary>
        /// <returns><c>true</c>, if the source value based on all input data
        /// could be evaluated, else <c>false</c>.</returns>
        protected bool UpdateSourceValue()
        {
            if (_isUpdatingSourceValue)
            {
                return(false);
            }
            _isUpdatingSourceValue = true;
            bool sourceValueValid = false;

            try
            {
                object result;
                bool   copy = false;
                lock (_syncObj)
                {
                    IDataDescriptor[] values;
                    if (!GetSourceValues(out values))
                    {
                        // Do nothing if not all necessary child bindings can be resolved at the current time
                        return(false);
                    }
                    if (_converter == null)
                    {
                        throw new XamlBindingException("MultiBindingMarkupExtension: Converter must be set");
                    }
                    Type targetType = _targetDataDescriptor == null ? typeof(object) : _targetDataDescriptor.DataType;
                    if (!_converter.Convert(values, targetType, ConverterParameter, out result))
                    {
                        return(false);
                    }
                    copy = values.Any(dd => dd != null && ReferenceEquals(dd.Value, result));
                    IsSourceValueValid = sourceValueValid = true;
                }
                object oldValue = _evaluatedSourceValue.SourceValue;
                // Set the binding's value outside the lock to comply with the MP2 threading policy
                _evaluatedSourceValue.SourceValue = new ValueDataDescriptor(copy ? MpfCopyManager.DeepCopyCutLVPs(result) : result);
                if (oldValue != null)
                {
                    MPF.TryCleanupAndDispose(oldValue);
                }
                return(true);
            }
            finally
            {
                IsSourceValueValid     = sourceValueValid;
                _isUpdatingSourceValue = false;
            }
        }
Пример #6
0
        private object ConvertValue(IList <object> values, Type targetType, IMultiValueConverter converter)
        {
            var culture   = CultureInfo.CurrentCulture;
            var converted = converter.Convert(values, targetType, ConverterParameter, culture);

            if (converted == BindingOperations.DoNothing)
            {
                return(converted);
            }

            if (converted == AvaloniaProperty.UnsetValue)
            {
                converted = FallbackValue;
            }

            return(converted);
        }
        internal static IMultiValueConverter AssertConvert <TConvertedValue>(this IMultiValueConverter converter, object[] values, object parameter, TConvertedValue expectedConvertedValue, bool twoWay = false, bool backOnly = false, CultureInfo culture = null)
        {
            Assert.That(converter?.Convert(values, typeof(TConvertedValue), parameter, culture), Is.EqualTo(backOnly ? BindableProperty.UnsetValue : expectedConvertedValue));
            var convertedBackValues = converter?.ConvertBack(expectedConvertedValue, null, parameter, culture);

            if (twoWay || backOnly)
            {
                Assert.That(convertedBackValues.Length, Is.EqualTo(values.Length));
                for (int i = 0; i < values.Length; i++)
                {
                    Assert.That(convertedBackValues[i], Is.EqualTo(values[i]));
                }
            }
            else
            {
                Assert.That(convertedBackValues, Is.Null);
            }
            return(converter);
        }
Пример #8
0
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     if (_multiValueConverter != null)
     {
         value = _multiValueConverter.Convert(value as object[], targetType, parameter, culture);
     }
     if (!string.IsNullOrWhiteSpace(_stringFormat))
     {
         var array = value as object[];
         // ReSharper disable once ConvertIfStatementToNullCoalescingExpression
         if (array != null)
         {
             value = string.Format(_stringFormat, array);
         }
         else
         {
             value = string.Format(_stringFormat, value);
         }
     }
     return(value);
 }
Пример #9
0
 public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
 {
     _values = (object[])values.Clone();
     return(_converter.Convert(values, targetType, parameter, culture));
 }
Пример #10
0
 /// <summary>
 /// 绑定多个指定的值到属性,并使用指定的转换器。
 /// </summary>
 /// <param name="value">绑定的值。</param>
 /// <param name="converter">更新时,值转换器。</param>
 /// <param name="convertParameter">转换参数。</param>
 /// <param name="culture">区域信息。</param>
 /// <returns>返回已绑定的 <see cref="Forms.Binding"/> 实例。</returns>
 /// <exception cref="ArgumentException">给定数据为null时引发。</exception>
 /// <exception cref="ArgumentNullException">控件属性是已绑定到数据或<see cref="Forms.Binding"/> 未指定的有效列时引发。</exception>
 public IBindableProperty Binding(MultiBindableValue value, IMultiValueConverter converter, object convertParameter = null, CultureInfo culture = null)
 {
     BindingCore(value, i => converter.Convert((object[])i, bindingProperty.PropertyType, converter, culture), i => converter.ConvertBack(i, value.ValueTypes, convertParameter, culture));
     return(this);
 }
        internal static string BuildToolTip(ModelItem entry, IMultiValueConverter displayNameConverter, CultureInfo culture)
        {
            string result = null;
            if (null != entry && null != displayNameConverter)
            {
                StringBuilder sb = new StringBuilder();
                int indent = 0;
                ModelItem currentEntry = entry;
                while (currentEntry != null)
                {
                    if (null != currentEntry.Properties["Variables"])
                    {
                        ++indent;
                    }
                    currentEntry = currentEntry.Parent;
                }

                while (entry != null)
                {
                    if (null != entry.Properties["Variables"])
                    {
                        if (sb.Length != 0)
                        {
                            sb.Insert(0, "/");
                            sb.Insert(0, " ", --indent);
                            sb.Insert(0, Environment.NewLine);
                        }
                        var input = new object[] { entry, null != entry.Properties["DisplayName"] ? entry.Properties["DisplayName"].Value : null, (double)short.MaxValue };
                        sb.Insert(0, displayNameConverter.Convert(input, typeof(string), null, culture));
                    }
                    entry = entry.Parent;
                }
                result = sb.ToString();
            }
            return result;
        }
Пример #12
0
        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return(DependencyProperty.UnsetValue);
            }
            object[] unconvertedValues = (object[])value;
            object   converter         = Converter;

            object result = unconvertedValues;

            if (converter != null)
            {
                object converterParameter = ConverterParameter;
                SourceValuesErrors = null;
                try
                {
                    IMultiValueConverter multiConverter = converter as IMultiValueConverter;
                    if (multiConverter != null)
                    {
                        result = multiConverter.Convert(unconvertedValues, targetType, converterParameter, culture);
                    }
                    else
                    {
                        IValueConverter singleConverter = converter as IValueConverter;
                        if (singleConverter != null)
                        {
                            result = singleConverter.Convert(unconvertedValues[0], targetType, converterParameter, culture);
                        }
                    }
                }
                catch (Exception ex)
                {
                    SourceValuesErrors = new MultiBindingValidationError[] { new MultiBindingValidationError(ex) };
                    if (MultiBinding.ValidatesOnExceptions)
                    {
                        return(DependencyProperty.UnsetValue);
                    }
                    throw;
                }
            }


            if (result != DependencyProperty.UnsetValue)
            {
                String format = StringFormat;
                if (format != null)
                {
                    format = Regex.Replace(format, @"%(\d+)", "{$1}");
                    if (result is Object[])
                    {
                        result = String.Format(culture, format, (Object[])result);
                    }
                    else
                    {
                        result = String.Format(culture, format, result);
                    }
                }
            }
            return(result);
        }
Пример #13
0
 private object DoConvert(object[] obj)
 {
     return(_converter.Convert(obj, typeof(string), null, null));
 }
 /// <summary>
 /// Runs Convert with TargetType, Parameter and Culture = null
 /// </summary>
 /// <typeparam name="TOutput">The expected output</typeparam>
 /// <param name="valueConverter">The value converter</param>
 /// <param name="inputs">The inputs to use for the converter</param>
 /// <returns></returns>
 public static TOutput Convert <TOutput>(this IMultiValueConverter valueConverter, object[] inputs)
 {
     return((TOutput)valueConverter.Convert(inputs, null, null, null));
 }
Пример #15
0
 public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
 {
     return(_privelegedToTrueConverter.Convert(values, targetType, parameter, culture) as bool? == true
         ? Visibility.Visible
         : Visibility.Collapsed);
 }