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 class Timeconver:ivalueconverter { When the value is propagated from the binding source to the binding target, the method is called convert public object Convert (object value, Type targetType, object parameter, CultureInfo Culture) { if (value = = null) return dependencyproperty.unsetvalue; DateTime date = (datetime) value; Return date. ToString ("Yyyy-mm-dd"); } This method is called when a value is propagated from the binding target to the binding source Convertback public Object Convertback (object value, Type targetType, object parameter, CultureInfo culture) { string str = value As String; DateTime txtdate; if (Datetime.tryparse (str, out txtdate)) { return txtdate; } return dependencyproperty.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> <local:timeconver x:key= "Cvtdate"/></window.resources>
Specifying binding values in XAML uses a custom converter transform
<textbox x:name= "TextBox" text= "{Binding Elementname=dateone,path=selecteddate,converter={staticresource Cvtdate}} " horizontalalignment=" left " height=" "margin=" 85,105,0,0 " textwrapping=" Wrap " Verticalalignment= "Top" width= "183"/>
XAML file Contents:
<Grid> <datepicker x:name= "Dateone" horizontalalignment= "left" margin= "85,50,0,0" Verticalalignment= "Top" width= "183" selecteddateformat= "Long"/> <textbox x:name= "TextBox" text= "{ Binding Elementname=dateone,path=selecteddate,converter={staticresource Cvtdate}} " horizontalalignment=" left " height=" margin= "85,105,0,0" textwrapping= "Wrap" verticalalignment= "Top" width= "183"/> < Label x:name= "label" content= "selection Result:" Horizontalalignment= "left" margin= "19,105,0,0" verticalalignment= "Top"/> <label x:name= "Label1" content= "{Binding elementname=dateone,path=text}" Horizontalalignment= "left" margin= "85,145,0,0" verticalalignment= "Top"/></grid>
WPF Binding Value Converter Valueconverter usage introduction (i)