コード例 #1
0
        /// <summary>
        /// Returns the target type for a conversion operation.
        /// </summary>
        /// <param name="converterIndex">The index of the current converter about to be executed.</param>
        /// <param name="finalTargetType">The 'targetType' argument passed into the conversion method.</param>
        /// <param name="convert">Pass true if calling from the Convert method, or false if calling from ConvertBack.</param>
        protected virtual Type GetTargetType(int converterIndex, Type finalTargetType, bool convert)
        {
            // If the current converter is not the last/first in the list,
            // get a reference to the next/previous converter.
            IValueConverter nextConverter = null;

            if (convert)
            {
                if (converterIndex < Converters.Count - 1)
                {
                    nextConverter = Converters[converterIndex + 1];
                    if (nextConverter == null)
                    {
                        throw new InvalidOperationException(
                                  "The Converters collection of the ValueConverterGroup contains a null reference at index: "
                                  + (converterIndex + 1));
                    }
                }
            }
            else
            {
                if (converterIndex > 0)
                {
                    nextConverter = Converters[converterIndex - 1];
                    if (nextConverter == null)
                    {
                        throw new InvalidOperationException(
                                  "The Converters collection of the ValueConverterGroup contains a null reference at index: "
                                  + (converterIndex - 1));
                    }
                }
            }

            if (nextConverter != null)
            {
                ValueConversionAttribute conversionAttribute = cachedAttributes[nextConverter];

                // If the Convert method is going to be called, we need to use the SourceType of the next
                // converter in the list.  If ConvertBack is called, use the TargetType.
                return(convert ? conversionAttribute.SourceType : conversionAttribute.TargetType);
            }

            // If the current converter is the last one to be executed return the
            // target type passed into the conversion method.
            return(finalTargetType);
        }
コード例 #2
0
        private object BackwardConverterCall(IValueConverter _converter, object _value, object _parameter, CultureInfo _culture)
        {
            if (_converter == null)
            {
                return(null);
            }

            object[] attribs = _converter.GetType().GetCustomAttributes(typeof(ValueConversionAttribute), false);
            if (attribs != null && attribs.Length == 1)
            {
                ValueConversionAttribute vca = attribs[0] as ValueConversionAttribute;
                if (vca != null)
                {
                    Type currentSourceType = vca.SourceType;
                    return(_converter.ConvertBack(_value, currentSourceType, _parameter, _culture));
                }
            }

            return(null);
        }