Example #1
0
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="property">The property.</param>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        public object GetValue <T>(IDependencyProperty property, string input)
        {
            if (input == null)
            {
                return(null);
            }

            try
            {
                Type t = property.PropertyType;
                if (t == typeof(int))
                {
                    int result;
                    if (int.TryParse(input, NumberStyles.Integer, CultureInfo.InvariantCulture, out result))
                    {
                        return(result);
                    }
                }
                else
                {
                    if (t == typeof(string))
                    {
                        return(input);
                    }
                    if (t == typeof(bool))
                    {
                        bool flag;
                        switch (input)
                        {
                        case "true":
                            return(true);

                        case "false":
                            return(false);
                        }
                        if (bool.TryParse(input, out flag))
                        {
                            return(flag);
                        }
                    }
                    else
                    {
                        T output;
                        //if (t == typeof(Color))
                        //{
                        //    return GetColorValue<T>(input, t);
                        //}
                        TypeConverter converter;
                        Type          tc = property.TypeConverter;
                        if (tc != null)
                        {
                            converter = (TypeConverter)Activator.CreateInstance(tc);
                        }
                        else
                        {
                            converter = TypeDescriptor.GetConverter(property.PropertyType);
                        }
                        if (((converter != null) && converter.CanConvertFrom(typeof(string))) && converter.CanConvertTo(typeof(string)))
                        {
                            return((T)converter.ConvertFromInvariantString(input));
                        }
                        if (SerializationUtilities.TryGetValueFromBinaryForm <T>(input, out output))
                        {
                            return(output);
                        }
                    }
                }
            }
            catch { }

            return(default(T));
        }