It uses a DateTimeTypeConverter from the base class library ( EDIT: Well, it could use TypeConverter, however, it looks like from @DeviantSeev answer that they didn't.)
The default modes you are talking about are actually TypeConverters ( MSDN ), and they have been part of the .NET Framework since version 2.0, and they are used through the base class libraries. Another example of TypeConverters in WPF are the ThicknessTypeConverter properties for Padding , Margin and BorderThickness . It converts a comma-delimited string to a Thickness object.
There are many articles if you want to understand them further .
There are two parts to using TypeConverter - implementing the class, and then labeling your properties / types with TypeConverterAttribute .
For example, I recently had a user control that required the char[] that I wanted to install from Xaml , for example:
<AutoCompleteTextBox MultiInputDelimiters=",;. " />
Using
[TypeConverter(typeof(CharArrayTypeConverter))] public char[] MultiInputDelimiters { get { return (char[])GetValue(MultiInputDelimitersProperty); } set { SetValue(MultiInputDelimitersProperty, value); } }
Implementation
public class CharArrayTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return (Type.GetTypeCode(sourceType) == TypeCode.String); } public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value is string) return ((string)value).ToCharArray(); return value; } }
When to use TypeConverter ?
You can only use TypeDescriptors if you are writing a custom control, since you need to mark the property with TypeDescriptorAttribute . Also, I would use only TypeConverter if the conversion is rather straightforward - as in the example above, where I have a string and you want char[] , or if there are several possible formats from which I want to convert.
You write IValueConverter when you need extra flexibility about how a value is converted by manipulating it with data or passing a parameter. For example, a very frequent action in WPF converts a bool to Visibility ; There are three possible ways out of such a conversion ( Visible , Hidden , Collapsed ) and with only two inputs ( true , false ) this is difficult to solve in TypeConverter .
In my applications, to achieve these two inputs for three output tasks, I wrote one BoolToVisibilityConverter with TrueValue and FalseValue properties, and then I instantiated it three times in my global ResourceDictionary . I will send a sample code tomorrow morning, I am not in front of me right now. .
[ValueConversion(typeof(bool), typeof(Visibility))] public class BooleanToVisibilityConverter : IValueConverter { public Visibility FalseCondition { get; set; } public Visibility TrueCondition { get; set; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return ((bool)value) ? TrueCondition : FalseCondition; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if ((bool)value) return TrueCondition; return FalseCondition; } } <converters:BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" FalseCondition="Collapsed" TrueCondition="Visible"/> <converters:BooleanToVisibilityConverter x:Key="BoolToVisibilityCollapsedConverter" FalseCondition="Visible" TrueCondition="Collapsed"/> <converters:BooleanToVisibilityConverter x:Key="BoolToVisibilityHiddenConverter" FalseCondition="Visible" TrueCondition="Hidden"/> <converters:BooleanToVisibilityConverter x:Key="BoolToVisibilityHiddenWhenFalseConverter" FalseCondition="Hidden" TrueCondition="Visible"/>