/// <summary> /// Converts a value. The data binding engine calls this method when it propagates a value from the binding source to the binding target. /// </summary> /// <param name="value">The value produced by the binding source.</param> /// <param name="targetType">The type of the binding target property.</param> /// <param name="parameter">The converter parameter to use.</param> /// <param name="culture">The culture to use in the converter.</param> /// <returns> /// A converted value.If the method returns null, the valid null value is used.A return value of <see cref="T:System.Windows.DependencyProperty"></see>.<see cref="F:System.Windows.DependencyProperty.UnsetValue"></see> indicates that the converter produced no value and that the binding uses the <see cref="P:System.Windows.Data.BindingBase.FallbackValue"></see>, if available, or the default value instead.A return value of <see cref="T:System.Windows.Data.Binding"></see>.<see cref="F:System.Windows.Data.Binding.DoNothing"></see> indicates that the binding does not transfer the value or use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue"></see> or default value. /// </returns> public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { DateTime dateTime; if (value is DateTime) { dateTime = (DateTime)value; // if the parameter isn't used or isn't a string, // just convert to a short datetime IFormatProvider format = culture.GetFormat(targetType) as IFormatProvider; if (parameter == null || !(parameter is String)) { return dateTime.ToString("d", format); } else { // Attempt to get the format provider for the culture specified return dateTime.ToString((string)parameter, format); } } // Can't do anything with it so jsut return the value Debug.WriteLine("Failed to convert DateTime to string, value is not a DateTime value."); return value; }
/// <summary> /// converts datetime from format yyyy/MM/dd HH:mm:ss /// </summary> /// <param name="context"></param> /// <param name="culture"></param> /// <param name="value"></param> /// <returns></returns> public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value is string) { string DateString = (string)value; try { if (culture == null) { return DateTime.Parse(DateString); } else { DateTimeFormatInfo info = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo)); info.ShortDatePattern = "yyyy/MM/dd HH:mm:ss"; return DateTime.Parse(DateString, info); } } catch { throw new FormatException(DateString + " is not a valid DateTime value. The format should be yyyy/MM/dd HH:mm:ss"); } } return base.ConvertFrom(context, culture, value); }
/// <summary> /// Determines whether the specified string is a valid percent value. /// </summary> /// <param name="p_strVal">The string value.</param> /// <param name="provider">The provider.</param> /// <returns> /// <c>true</c> if the specified string is a valid percent value; otherwise, <c>false</c>. /// </returns> public static bool IsPercentString(string p_strVal, System.IFormatProvider provider) { if (p_strVal == null) { return false; } NumberFormatInfo l_Info; if (provider == null) { l_Info = CultureInfo.CurrentCulture.NumberFormat; } else { l_Info = (NumberFormatInfo)provider.GetFormat(typeof(NumberFormatInfo)); } return p_strVal.IndexOf(l_Info.PercentSymbol) != -1; }