WPF Binding Value Converter Valueconverter usage introduction (i)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.