Example #1
0
        /// <summary>
        /// Converts back, by looking in the dictionary for matching value and returning the key of the matched value
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            object result = ConvertBackFallbackValue;

            if (IsNullable && Equals(NullValue, value))
            {
                result = null;
            }
            else
            {
                var it = GetEnumerator();
                while (it.MoveNext())
                {
                    if (Equals(value, it.Value))
                    {
                        result = it.Key;
                        break;
                    }
                }
            }

            result = ConverterSpecialValue.Apply(result, value, parameter);

            return(result);
        }
Example #2
0
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            object value  = (values.Length > 0)? values[0]: null;
            bool   equals = false;

            if (values.Length > 1)
            {
                var compareTo = ConverterSpecialValue.Apply(value, value, parameter);

                for (int i = 1; i < values.Length; i++)
                {
                    var value2 = values[i];
                    value2 = ConverterSpecialValue.Apply(value2, value2, parameter);
                    equals = Equals(compareTo, value2);
                    if (!equals)
                    {
                        break;
                    }
                }
            }

            var result = (equals != IsNegative) ? TrueValue : FalseValue;

            result = ConverterSpecialValue.Apply(result, value, parameter);

            //It seems that with multi-binding (as opposed to regular binding),
            //WPF doesn't convert result to target-type automatically.
            object castedResult;

            ConvertHelper.TryChangeType(result, targetType, out castedResult, culture);

            return(castedResult);
        }
Example #3
0
        /// <summary>
        /// Converts an input value by looking it up as a key in dictionary and returning the mapped value for it.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var key = value;

            if (ReferenceEquals(null, value))
            {
                return((IsNullable) ? NullValue : FallbackValue);
            }

            var foundValue = FallbackValue;

            if (this.Contains(key))
            {
                foundValue = this[key];
            }
            else if (this.Contains(key.ToString()))
            {
                foundValue = this[key.ToString()];
            }

            var converter = (foundValue as IValueConverter);

            if (ConvertAlsoFoundValueConverter && converter != null)
            {
                foundValue = converter.Convert(value, targetType, parameter, culture);
            }

            return(ConverterSpecialValue.Apply(foundValue, value, parameter));
        }
Example #4
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            object result;
            bool   isNegative = (parameter is bool) ? (bool)parameter : IsNegative;

            if (ReferenceEquals(null, value))
            {
                result = (IsNullable) ? NullValue : IsNegative ? TrueValue : FalseValue;
            }
            else
            {
                result = (IsInRange(value, _lower, _upper, culture) != isNegative) ? TrueValue : FalseValue;
            }

            return(ConverterSpecialValue.Apply(result, value, parameter));
        }
Example #5
0
        private object ConvertInternal(object value, Type targetType, object parameter, CultureInfo culture)
        {
            object result;

            if (IsNullable && ReferenceEquals(null, value))
            {
                result = NullValue;
            }
            else
            {
                bool isNegative = (parameter is bool) ? (bool)parameter : IsNegative;
                bool equals     = Equals(value, Boxed.True);
                result = (equals != isNegative) ? TrueValue : FalseValue;
            }

            return(ConverterSpecialValue.Apply(result, value, parameter));
        }
        public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            object result;

            if (IsNullable && ReferenceEquals(null, value))
            {
                result = NullValue;
            }
            else
            {
                var compareTo = CompareTo;
                compareTo = Equals(compareTo, ConverterSpecialValue.UseConverterParameter) ? parameter : compareTo;
                bool equals = Equals(value, compareTo);

                result = (equals != IsNegative) ? TrueValue : FalseValue;
            }
            return(ConverterSpecialValue.Apply(result, value, parameter));
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool isNull  = ReferenceEquals(null, value);
            var  isEmpty = false;

            if (!isNull)
            {
                var enumerable = (value as IEnumerable)?.GetEnumerator();
                using (enumerable as IDisposable)
                {
                    bool hasValue = enumerable?.MoveNext() ?? false;
                    isEmpty = !hasValue;
                }
            }
            bool isNullOrEmpty = (isNull && Mode == ComparisonMode.IsNullOrEmpty || isEmpty);
            var  result        = (isNullOrEmpty != IsNegative) ? TrueValue : FalseValue;

            return(ConverterSpecialValue.Apply(result, value, parameter));
        }
        public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            object result;
            var    compareTo = IsNegative ? FalseValue: TrueValue;

            compareTo = ConverterSpecialValue.Apply(compareTo, value, parameter);
            if (Equals(value, compareTo))
            {
                result = CompareTo;
            }
            else if (IsNullable && Equals(value, ConverterSpecialValue.Apply(NullValue, value, parameter)))
            {
                result = null;
            }
            else
            {
                result = value;
            }

            return(ConverterSpecialValue.Apply(result, value, parameter));
        }
Example #9
0
 public ConverterSpecialExtension(ConverterSpecialValue specialValue)
 {
     Value = specialValue;
 }