예제 #1
0
        /// <summary>
        /// Convert Min and Max values to string if step type is set to one of the DateTime type
        /// </summary>
        /// <param name="context">Descriptor context.</param>
        /// <param name="culture">Culture information.</param>
        /// <param name="value">Value to convert.</param>
        /// <param name="destinationType">Convertion destination type.</param>
        /// <returns>Converted object.</returns>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (context != null && context.Instance != null)
            {
                // Convert to string
                if (destinationType == typeof(string))
                {
                    DateTimeIntervalType intervalType = DateTimeIntervalType.Auto;
                    double interval = 0;

                    // Get IntervalType property using reflection
                    PropertyInfo propertyInfo = context.Instance.GetType().GetProperty("IntervalType");
                    if (propertyInfo != null)
                    {
                        intervalType = (DateTimeIntervalType)propertyInfo.GetValue(context.Instance, null);
                    }

                    // Get Interval property using reflection
                    propertyInfo = context.Instance.GetType().GetProperty("Interval");
                    if (propertyInfo != null)
                    {
                        interval = (double)propertyInfo.GetValue(context.Instance, null);
                    }

                    // Try to get interval information from the axis
                    if (intervalType == DateTimeIntervalType.Auto)
                    {
                        // Get object's axis
                        Axis axis = null;
                        if (context.Instance is Axis)
                        {
                            axis = (Axis)context.Instance;
                        }
                        else
                        {
                            MethodInfo methodInfo = context.Instance.GetType().GetMethod("GetAxis");
                            if (methodInfo != null)
                            {
                                // Get axis object
                                axis = (Axis)methodInfo.Invoke(context.Instance, null);
                            }
                        }

                        // Get axis value type
                        if (axis != null)
                        {
                            intervalType = axis.GetAxisIntervalType();
                        }
                    }

                    // Convert value to date/time string
                    if (context.Instance.GetType() != typeof(StripLine) || interval == 0)
                    {
                        if (intervalType != DateTimeIntervalType.Number && intervalType != DateTimeIntervalType.Auto)
                        {
                            // Covert value to date/time
                            if (intervalType < DateTimeIntervalType.Hours)
                            {
                                return(DateTime.FromOADate((double)value).ToShortDateString());
                            }
                            return(DateTime.FromOADate((double)value).ToString("g", System.Globalization.CultureInfo.CurrentCulture));
                        }
                    }
                }
            }
            return(base.ConvertTo(context, culture, value, destinationType));
        }
예제 #2
0
        /// <summary>
        /// Convert Min and Max values from string if step type is set to one of the DateTime type
        /// </summary>
        /// <param name="context">Descriptor context.</param>
        /// <param name="culture">Culture information.</param>
        /// <param name="value">Value to convert from.</param>
        /// <returns>Converted object.</returns>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            object result          = null;
            bool   convertFromDate = false;
            string stringValue     = value as string;

            // If context interface provided check if we are dealing with DateTime values
            if (context != null && context.Instance != null)
            {
                DateTimeIntervalType intervalType = DateTimeIntervalType.Auto;

                // Get intervalType property using reflection
                PropertyInfo propertyInfo = context.Instance.GetType().GetProperty("intervalType");
                if (propertyInfo != null)
                {
                    intervalType = (DateTimeIntervalType)propertyInfo.GetValue(context.Instance, null);
                }

                // Try to get interval information from the axis
                if (intervalType == DateTimeIntervalType.Auto)
                {
                    // Get object's axis
                    Axis axis = null;
                    if (context.Instance is Axis)
                    {
                        axis = (Axis)context.Instance;
                    }
                    else
                    {
                        MethodInfo methodInfo = context.Instance.GetType().GetMethod("GetAxis");
                        if (methodInfo != null)
                        {
                            // Get axis object
                            axis = (Axis)methodInfo.Invoke(context.Instance, null);
                        }
                    }

                    // Get axis value type
                    if (axis != null)
                    {
                        intervalType = axis.GetAxisIntervalType();
                    }
                }

                if (stringValue != null && intervalType != DateTimeIntervalType.Number && intervalType != DateTimeIntervalType.Auto)
                {
                    convertFromDate = true;
                }
            }

            // Try to convert from double string
            try
            {
                result = base.ConvertFrom(context, culture, value);
            }
            catch (ArgumentException)
            {
                result = null;
            }
            catch (NotSupportedException)
            {
                result = null;
            }

            // Try to convert from date/time string
            if (stringValue != null && (convertFromDate || result == null))
            {
                DateTime valueAsDate;
                bool     parseSucceed = DateTime.TryParse(stringValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out valueAsDate);

                if (parseSucceed)
                {
                    // Succeded converting from date format
                    return(valueAsDate.ToOADate());
                }
            }

            // Call base converter
            return(base.ConvertFrom(context, culture, value));
        }