In WPF, Silverlight, and Windows Phone program development, it is often necessary to make specific transformations of the bound data, such as the date of the DateTime type to YYYYMMDD, and, if one value is different depending on the other set of values, At this point, we need to customize our own converter.
The. Net Framework provides two interfaces for converter interfaces, single-value conversionsIValueConverterand multi-value conversion interfacesImultivalueconverter, they all belong to the System.Windows.Data namespace, in assembly PresentationFramework.dll. Both of theseValue converters are sub-cultural. which method convertand Convertback both have information that indicates the cultureThe culture parameter. If the culture information is not related to the transformation, the parameter can be ignored in the custom converter.
One, single-value conversion example, IValueConverter
1. When the value is propagated from the binding source to the binding target, the method is called convert
2. Call this method Convertback when the value is propagated from the binding target to the binding source, and the implementation of the method Convertback must be a reverse implementation of the method convert.
/// <summary> ///Custom Event Conversions/// </summary> Public classTimeconver:ivalueconverter {//when the value is propagated from the binding source to the binding target, the method is called convert Public ObjectConvert (ObjectValue, Type TargetType,Objectparameter, CultureInfo culture) { if(Value = =NULL) returnDependencyproperty.unsetvalue; DateTime Date=(DateTime) value; returnDate. ToString ("YYYY-MM-DD"); } //This method is called when the value is propagated from the binding target to the binding source Convertback Public ObjectConvertback (ObjectValue, Type TargetType,Objectparameter, CultureInfo culture) { stringstr = value as string; DateTime txtdate; if(Datetime.tryparse (str, outtxtdate)) { returntxtdate; } returnDependencyproperty.unsetvalue; } }
Note: The return value dependencyproperty.unsetvalue indicates that the converter has not generated any values.
Referencing the Timeconver namespace in XAML
Xmlns:local= "Clr-namespace:audiodemo.view"
Defining resources in XAML
< window.resources > < x:key= "cvtdate"/></window.resources>
Specifying binding values in XAML uses a custom converter transform
<TextBoxx:name= "TextBox"Text="{Binding Elementname=dateone,path=selecteddate,converter={staticresource cvtdate}}"HorizontalAlignment= "Left"Height= "All"Margin= "85,105,0,0"textwrapping= "Wrap"VerticalAlignment= "Top"Width= "183"/>
XAML file Contents:
<Grid> <DatePickerx:name= "Dateone"HorizontalAlignment= "Left"Margin= "85,50,0,0"VerticalAlignment= "Top"Width= "183"Selecteddateformat= "Long"/> <TextBoxx:name= "TextBox"Text="{Binding Elementname=dateone,path=selecteddate,converter={staticresource cvtdate}}"HorizontalAlignment= "Left"Height= "All"Margin= "85,105,0,0"textwrapping= "Wrap"VerticalAlignment= "Top"Width= "183"/> <Labelx:name= "Label"Content= "Select result:"HorizontalAlignment= "Left"Margin= "19,105,0,0"VerticalAlignment= "Top"/> <Labelx:name= "Label1"Content="{Binding Elementname=dateone,path=text}"HorizontalAlignment= "Left"Margin= "85,145,0,0"VerticalAlignment= "Top"/></Grid>
Operation Result:
Before using conversion: After using a custom converter conversion:
For more information:
WPF Binding Value Converter Valueconverter Introduction to use (ii)-imultivalueconverter
WPF Binding Value Converter Valueconverter usage introduction (i)