WPF Value Converter IValueConverter Example

Source: Internet
Author: User

Reprint: Http://blog.163.com/[email protected]/blog/static/37140526201085113430862/

A value converter can convert one type to another type. For example, bind to a string representing a picture address, want to display a picture, store the data as a floating-point type, but in the form of a currency, and save the date in a DateTime format, use the calender control when displayed on the interface, and so on.
Here is a simple example of getting the current time of the system, showing "now is 2010-xx-xx xx:xx;xx".
Code for XAML:

1  2<window x:class="Velueconvertertest.window1"3xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"4xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"5Xmlns:local="clr-namespace:velueconvertertest"6title="Window1"height=" -"Width=" -">7<Window.Resources>8<local:dateconverter x:key="DateConverter"/>9</Window.Resources>Ten<Grid> One<label margin="53,104,45,130"Name="Label1"Content="{Binding Converter={staticresource dateconverter}}"/> A</Grid> -</Window>

A XAML file defines a dateconverter resource. Point to the DateConverter class in the CS file.

 
1 12 23 34 45 56 67 78 89 9Ten Ten One  One A  A -  - -  - the  the -  - -  - -  - +  + -  - +  + A  A at  at -  - -  - -  - -  - -  - in  in -  - to  to +  + -  - the  the *  * $  $Panax Notoginseng Panax Notoginseng -  - the  the +  + A  A the  the +  + -  - $  $ $  $ -  - -  - the  the -  -Wuyi usingSystem; the usingSystem.Collections.Generic; - usingSystem.Linq; Wu usingSystem.Text; - usingSystem.Windows; About usingSystem.Windows.Controls; $ usingSystem.Windows.Data; - usingSystem.Windows.Documents; - usingSystem.Windows.Input; - usingSystem.Windows.Media; A usingSystem.Windows.Media.Imaging; + usingSystem.Windows.Navigation; the usingSystem.Windows.Shapes; - usingSystem.Globalization; $  the namespacevelueconvertertest the { the      Public Partial classWindow1:window the     { -          PublicDateTime Nowtime {Get;Set; } in          PublicWindow1 () the         { the InitializeComponent (); AboutNowtime =DateTime.Now; theLabel1. DataContext =Nowtime; the         } the     } +  -    //Defining a value converter the[Valueconversion (typeof(DateTime),typeof(String))]Bayi      Public classDateconverter:ivalueconverter the     { the          Public ObjectConvert (ObjectValue, Type TargetType,Objectparameter, CultureInfo culture) -         { -DateTime date =(DateTime) value; the             return " now is"+date. ToString (); the         } the  the          Public ObjectConvertback (ObjectValue, Type TargetType,Objectparameter, CultureInfo culture) -         { the             stringstrvalue =value. ToString (); the DateTime resultdatetime; the             if(Datetime.tryparse (strvalue, outresultdatetime))94             { the                 returnResultdatetime; the             } the             returnvalue;98         } About     } -}

The difference between convert and Convertback:
The CONVERT function represents a value conversion from a data source to a destination, and the Convertback function represents a value conversion from a target to a data source. Therefore, if the binding pattern is one-time or one-way binding, you simply implement the CONVERT function, and if the binding pattern is two-way bound, you need to implement the convert and Convertback functions.

The converter of the label is defined in XAML, and when the binding is executed, WPF will put the pre-conversion value, such as nowtime in this example, as the input value of the converter function convert, and display the return value on the label control.

WPF Value Converter IValueConverter Example

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.