Convert sorting in Silverlight

Source: Internet
Author: User

1. Convert to true. False is used to bind the ischecked attribute similar to that in the checkbox:

Public class boolconverter: ivalueconverter <br/>{< br/> Public object convert (object value, type targettype, object parameter, cultureinfo Culture) <br/>{< br/> string S = (string) value; <br/> bool B; <br/> If (S = NULL | S. trim (). length = 0 | S = "0") <br/> B = false; <br/> else <br/> B = true; <br/> return B; <br/>}< br/> Public object convertback (object value, type targettype, object Pa Rameter, cultureinfo culture) <br/>{< br/> string strvalue = (bool) value? "1": "0"; <br/> return strvalue; <br/>}< br/>}

 

Ii. Time String Conversion

# Region is a date definition converter <br/> // defines a conversion class and is referenced as a resource on the page <br/>/* <br/> * ivalueconverter-value conversion interface, converts a value of one type to a value of another type. It provides a way to apply custom logic to binding <br/> * convert-Forward Converter. When you pass the value from the data source to the binding target, the Data Binding engine calls this method <br/> * convertback-reverse converter. When you pass a value from the bound destination to the data source, the Data Binding engine calls this method <br/> */<br/> // <summary> <br/> // forward converter. When you pass the value from the data source to the bound destination, the Data Binding engine calls this method <br/> /// </Summary> <br/> /// <Param name = "value"> value before conversion </Param> <br/> /// <Param name = "targettype"> converted type </param> <br/> /// <Param name = "parameter"> Converter parameters Used </param> <br/> // <Param name = "culture"> region information used by the converter </param> <br/> /// <returns> converted value </returns> <br/> public class datetimeconverter: ivalueconverter <br/>{< br/> Public object convert (object value, <br/> type Ta Rgettype, <br/> object parameter, <br/> cultureinfo culture) <br/>{< br/> datetime date = (datetime) value; <br/> return date. tostring ("yyyy-mm-dd"); <br/>}< br/> /// <summary> <br/> // reverse converter. When you pass a value from the bound destination to the data source, the Data Binding engine calls this method <br/> /// </Summary> <br/> /// <Param name = "value"> value before conversion </Param> <br/> /// <Param name = "targettype"> converted type </param> <br/> /// <Param name = "parameter"> Converter parameters Used </param> <br/> // <Param name = "culture"> region information used by the converter </param> <br/> /// <returns> converted value </returns> <br/> Public object convertback (object value, <br/> type targettype, <br/> object parameter, <br/> cultureinfo culture) <br/>{< br/> string strvalue = value. tostring (); <br/> datetime resultdatetime; <br/> If (datetime. tryparse (strvalue, out resultdatetime) <br/>{< br/> return resultdatetime; <br/>}< br/> return value; <br/>}< br/> # endregion

The usage is as follows:

First, add the following statement to the page for conversion:

Xmlns: Local = "CLR-namespace: XXX" // XXX is the namespace of your Silverlight project.

Then add:

<Usercontrol. Resources>
<Local: datetimeconverter X: Key = "datetimeconverter"/>

<Local: boolconverter
X: Key = "boolconverter
"/>

</Usercontrol. Resources>

<Checkbox X: Name = "CHK1" content = "I am checkbox" ischecked = "{binding database table field, convert = {staticresource boolconverter}" margin = "5"/>

Iii. Image Source attribute binding

Define a converter iconconverter class

// The icon is of the source type and the imagesource is of the target type.

[Valueconversion (typeof (icon), typeof (imagesource)]

// Inherits ivalueconverterpublic class iconconverter: ivalueconverter {public object convert (object value, type targettype, object parameter, cultureinfo culture) {icon = (icon) value; Bitmap bitmap = icon. tobitmap (); intptr hbitmap = bitmap. gethbitmap (); imagesource bitmapsource = imaging. createbitmapsourcefromhbitmap (hbitmap, intptr. zero, int32rect. empty, bitmapsizeoptions. fromemptyoptions ()); Return bitmapsource;} // The following function is used to implement the inverse operation of the preceding conversion, here we do not need to change imagesource to an icon, so we did not write a specific implementation of public object convertback (object value, type targettype, object parameter, cultureinfo culture) {Throw new notimplementedexception ();}} finally, rebind the data. 1. Add reference: xmlns: Local = "CLR-namespace: namespace of the converter" 2. Add Resource: <local: iconconverter X: key = "iconconverter"/> 3. Bind the converter data: <Image Source = "{binding Path = menuicon, converter = {staticresour Ce iconconverter }}"/> OK. The problem is fixed.

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.