Binding data conversion

Source: Internet
Author: User

Binding is a bridge between the source object and the target object. If there is a difference between the two types, in addition to implicit conversion of the system, such as conversion between basic data types, other types need to be displayed for conversion, the key is to implement the ivalueconverter interface.

The key points are as follows:

1. Implement the ivalueconverter Interface

Prototype:

// Abstract: // provides a method for applying custom logic to binding. Public interface ivalueconverter {// Abstract: // convert the value. //// Parameter: // value generated by the binding source. //// Targettype: // type of the target property to be bound. //// Parameter: // The converter parameter to be used. //// Culture: // The culture to use in the converter. //// Return result: // The converted value. If this method returns NULL, a valid null value is used. Object convert (object value, type targettype, object parameter, cultureinfo culture); // Abstract: // convert the value. //// Parameter: // value: // bind the value generated by the target. //// Targettype: // type to be converted. //// Parameter: // The converter parameter to be used. //// Culture: // The culture to use in the converter. //// Return result: // The converted value. If this method returns NULL, a valid null value is used. Object convertback (object value, type targettype, object parameter, cultureinfo culture );}

Convert refers to the conversion from the source data to the target data. convertback refers to the conversion from the target data to the source data.

Example:

Public class statetosourceconverter: ivalueconverter {// <summary> // convert the state of the source data to the bool type /// </Summary> Public object convert (object value, type targettype, object parameter, system. globalization. cultureinfo culture) {State Source = (state) value; Switch (source) {Case state. unknown: return NULL; case state. locked: Return false; Case state. available: Default: return NULL;} public object co Nvertback (object value, type targettype, object parameter, system. Globalization. cultureinfo culture) {bool? Target = (bool ?) Value; Switch (target) {case true: return state. available; case false: return state. Locked; Case NULL: Default: return state. Unknown ;}}}

2. Set the converter attribute in the binding object

When setting this attribute in XAML, you can directly reference the defined ivalueconverter class as a static resource, that is, add windowresource in XAML, for example:

    <Window.Resources>        <local:CategoryToSourceConverter x:Key="cts"/>        <local:StateToSourceConverter x:Key="sts"/>    </Window.Resources>

Reference conversion class:

<CheckBox IsThreeState="True" IsChecked="{Binding State,Converter={StaticResource sts}}"/>

Conclusion: The most important thing is that data conversion occurs when both the data source and the target object change. The convert and convertback methods are called.

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.