/// <summary>
        /// Modifies the source data before passing it to the target for display in the UI.
        /// </summary>
        /// <param name="value">The source data being passed to the target.</param>
        /// <param name="targetType">The <see cref="Type"/> of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the target dependency property.</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return(null);
            }

            object result       = null;
            Type   resourceType = value.GetType();

            // Simplest cases: The target type is object or same as the input.
            if (targetType.Equals(typeof(System.Object)) || resourceType.Equals(targetType))
            {
                return(value);
            }

            // Register missing type converters - this class will do this only once per appdomain.
            RegisterMissingTypeConverters.Register();

            // Is the type already known?
            if (!TypeConverters.ContainsKey(targetType))
            {
                var c = TypeDescriptor.GetConverter(targetType);

                if (targetType == typeof(Thickness))
                {
                    c = new TypeConverters.ThicknessConverter();
                }

                // Get the type converter and store it in the dictionary (even if it is NULL).
                TypeConverters.Add(targetType, c);
            }

            // Get the converter.
            TypeConverter conv = TypeConverters[targetType];

            // No converter or not convertable?
            if ((conv == null) || !conv.CanConvertFrom(resourceType))
            {
                return(null);
            }

            // Finally, try to convert the value.
            try
            {
                result = conv.ConvertFrom(value);
            }
            catch
            {
                result = null;
            }

            return(result);
        }
        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI.
        /// </summary>
        /// <param name="value">The source data being passed to the target.</param>
        /// <param name="targetType">The <see cref="Type"/> of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the target dependency property.</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;

            object result = null;
            Type resourceType = value.GetType();

            // Simplest cases: The target type is object or same as the input.
            if (targetType.Equals(typeof(System.Object)) || resourceType.Equals(targetType))
                return value;

            // Register missing type converters - this class will do this only once per appdomain.
            RegisterMissingTypeConverters.Register();

            // Is the type already known?
            if (!TypeConverters.ContainsKey(targetType))
            {
                var c = TypeDescriptor.GetConverter(targetType);

                if (targetType == typeof(Thickness))
                    c = new TypeConverters.ThicknessConverter();

                // Get the type converter and store it in the dictionary (even if it is NULL).
                TypeConverters.Add(targetType, c);
            }

            // Get the converter.
            TypeConverter conv = TypeConverters[targetType];

            // No converter or not convertable?
            if ((conv == null) || !conv.CanConvertFrom(resourceType))
                return null;

            // Finally, try to convert the value.
            try
            {
                result = conv.ConvertFrom(value);
            }
            catch
            {
                result = null;
            }

            return result;
        }
        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI.
        /// </summary>
        /// <param name="value">The source data being passed to the target.</param>
        /// <param name="targetType">The <see cref="Type"/> of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the target dependency property.</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;

            object result = null;
            Type resourceType = value.GetType();

            // Simplest cases: The target type is object or same as the input.
            if (targetType.Equals(typeof(System.Object)) || resourceType.Equals(targetType))
                return value;

#if SILVERLIGHT
            // Is the type already known?
            if (!TypeConverters.ContainsKey(targetType))
            {
                if (typeof(Enum).IsAssignableFrom(targetType))
                {
                    TypeConverters.Add(targetType, new TypeConverters.EnumConverter(targetType));
                }
                else
                {
                    Type converterType = null;
                    var attributes = targetType.GetCustomAttributes(typeof(TypeConverterAttribute), false);

                    if (attributes.Length == 1)
                    {
                        var converterAttribute = (TypeConverterAttribute)attributes[0];
                        converterType = Type.GetType(converterAttribute.ConverterTypeName);
                    }

                    if (converterType == null)
                    {
                        // Find a suitable "common" converter.
                        if (targetType == typeof(double))
                            converterType = typeof(TypeConverters.DoubleConverter);
                        else if (targetType == typeof(Thickness))
                            converterType = typeof(TypeConverters.ThicknessConverter);
                        else if (targetType == typeof(Brush))
                            converterType = typeof(TypeConverters.BrushConverter);
                        else
                            return value;
                    }

                    // Get the type converter and store it in the dictionary (even if it is NULL).
                    TypeConverters.Add(targetType, Activator.CreateInstance(converterType) as TypeConverter);
                }
            }
#else
            // Register missing type converters - this class will do this only once per appdomain.
            RegisterMissingTypeConverters.Register();

            // Is the type already known?
            if (!TypeConverters.ContainsKey(targetType))
            {
                var c = TypeDescriptor.GetConverter(targetType);

                if (targetType == typeof(Thickness))
                    c = new TypeConverters.ThicknessConverter();

                // Get the type converter and store it in the dictionary (even if it is NULL).
                TypeConverters.Add(targetType, c);
            }
#endif

            // Get the converter.
            TypeConverter conv = TypeConverters[targetType];

            // No converter or not convertable?
            if ((conv == null) || !conv.CanConvertFrom(resourceType))
                return null;

            // Finally, try to convert the value.
            try
            {
                result = conv.ConvertFrom(value);
            }
            catch
            {
                result = null;
            }

            return result;
        }
        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI.
        /// </summary>
        /// <param name="value">The source data being passed to the target.</param>
        /// <param name="targetType">The <see cref="Type"/> of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the target dependency property.</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return(null);
            }

            object result       = null;
            Type   resourceType = value.GetType();

            // Simplest cases: The target type is object or same as the input.
            if (targetType.Equals(typeof(System.Object)) || resourceType.Equals(targetType))
            {
                return(value);
            }

#if SILVERLIGHT
            // Is the type already known?
            if (!TypeConverters.ContainsKey(targetType))
            {
                if (typeof(Enum).IsAssignableFrom(targetType))
                {
                    TypeConverters.Add(targetType, new TypeConverters.EnumConverter(targetType));
                }
                else
                {
                    Type converterType = null;
                    var  attributes    = targetType.GetCustomAttributes(typeof(TypeConverterAttribute), false);

                    if (attributes.Length == 1)
                    {
                        var converterAttribute = (TypeConverterAttribute)attributes[0];
                        converterType = Type.GetType(converterAttribute.ConverterTypeName);
                    }

                    if (converterType == null)
                    {
                        // Find a suitable "common" converter.
                        if (targetType == typeof(double))
                        {
                            converterType = typeof(TypeConverters.DoubleConverter);
                        }
                        else if (targetType == typeof(Thickness))
                        {
                            converterType = typeof(TypeConverters.ThicknessConverter);
                        }
                        else if (targetType == typeof(Brush))
                        {
                            converterType = typeof(TypeConverters.BrushConverter);
                        }
                        else
                        {
                            return(value);
                        }
                    }

                    // Get the type converter and store it in the dictionary (even if it is NULL).
                    TypeConverters.Add(targetType, Activator.CreateInstance(converterType) as TypeConverter);
                }
            }
#else
            // Register missing type converters - this class will do this only once per appdomain.
            RegisterMissingTypeConverters.Register();

            // Is the type already known?
            if (!TypeConverters.ContainsKey(targetType))
            {
                var c = TypeDescriptor.GetConverter(targetType);

                if (targetType == typeof(Thickness))
                {
                    c = new TypeConverters.ThicknessConverter();
                }

                // Get the type converter and store it in the dictionary (even if it is NULL).
                TypeConverters.Add(targetType, c);
            }
#endif

            // Get the converter.
            TypeConverter conv = TypeConverters[targetType];

            // No converter or not convertable?
            if ((conv == null) || !conv.CanConvertFrom(resourceType))
            {
                return(null);
            }

            // Finally, try to convert the value.
            try
            {
                result = conv.ConvertFrom(value);
            }
            catch
            {
                result = null;
            }

            return(result);
        }