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);
        }
Esempio n. 2
0
        /// <summary>
        /// Converts a value by iterating in reverse order through the <see cref="Converters"/> collection and executing <see cref="IValueConverter.ConvertBack"/> method for each <see cref="IValueConverter"/> contained, next executing <see cref="IMultiValueConverter.ConvertBack"/> method of the <see cref="Converter"/>. The return value of the <see cref="IValueConverter.ConvertBack"/> method call for current <see cref="IValueConverter"/> in the iteration is passed as input value for processing next <see cref="IValueConverter"/> in the iteration or processing the <see cref="Converter"/>.
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetTypes">The array of types to convert to. The array length indicates the number and types of values that are suggested for the method to return.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>An array of values that have been converted from the target value back to the source values, meaning the result of processing the reversed chain of converters.</returns>
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            IMultiValueConverter converter = Converter;

            if (converter != null)
            {
                for (int i = converters.Count - 1; i >= 0; i--)
                {
                    value = converters[i].ConvertBack(value, typeof(object), parameter, culture);

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

                return(converter.ConvertBack(value, targetTypes, parameter, culture));
            }

            return(null);
        }
Esempio n. 3
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);
 }
Esempio n. 4
0
 public void TestName()
 {
     Assert.Throws <NotImplementedException>(() =>
                                             _converter.ConvertBack("ThomasTheTankEngine", null, null, null)
                                             );
 }