Two methods of implementing data type Conversions in XAML in WPF

Source: Internet
Author: User

Friends who are familiar with data binding know that when we get the data of an object in the model, we often need to convert it to the UI interface, for example, you store the gender of a person with a bool type, but the interface needs to be transformed and displayed as a male or female;

Today, the data binding part of the look again, here is a summary of it!

method One : When we define a class, which has a property of that class type, then if we just use simple data binding in XAML, the program will have a problem because the program does not correctly convert the properties of the class type of the class object. So we need to customize a transformation class that inherits TypeConverter and needs to rewrite a member function of the class ConvertFrom, and add a type conversion property for the class that needs to be converted, and finally use the static resource in XAML!

The sample code for the corresponding class is as follows:

    Human.cs

[TypeConverterAttribute (typeof (Childconvertertohuman))] public class Human {public string Name {get; set;} Public Human Child {get; set;} } public class Childconvertertohuman:typeconverter {public override object ConvertFrom ( ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { Human h = new Human (); String str = value. ToString (); H.name = str; return h; } }

MainWindow. xaml    

<window.resources > < x:key= "Human" Name= "hippie" child= "Tom"/> </ window.resources >

This allows us to correctly convert the data type of the child property of the human object;

this. FindResource ("human" as human; if NULL ) {     + hu. Child.name);}

method Two : The reason and method one is the same, still need to define a type conversion class, but need to inherit the class is the IValueConverter class, we need to rewrite the convert and Convertback functions (general rewrite convert can)

   Public classPerson { Public stringName {Get;Set; }  Public BOOLGender {Get;Set; } }     Public classConverter:ivalueconverter {/// <summary>        ///Model to UI/// </summary>        /// <param name= "value" ></param>        /// <param name= "TargetType" ></param>        /// <param name= "parameter" ></param>        /// <param name= "culture" ></param>        /// <returns></returns>         Public ObjectConvert (ObjectValue, Type TargetType,Objectparameter, System.Globalization.CultureInfo culture) {            BOOLGender = (BOOL) value; if(Gender = =true)            {                return "male"; }            Else            {                return "female"; }        }        /// <summary>        ///UI to Model/// </summary>        /// <param name= "value" ></param>        /// <param name= "TargetType" ></param>        /// <param name= "parameter" ></param>        /// <param name= "culture" ></param>        /// <returns></returns>         Public ObjectConvertback (ObjectValue, Type TargetType,Objectparameter, System.Globalization.CultureInfo culture) {            Throw Newnotimplementedexception (); }    }
    <window.resources>        <Local:converterx:key= "Converter"/>    </window.resources>    <Grid>        <StackPanel>            <StackPanelx:name= "SP"Orientation= "Horizontal">                <TextBlockText="{Binding Name}"FontSize= " the"Margin= "0,0,24,0"/>                <TextBlockText="{Binding Gender, Converter={staticresource Converter}}"FontSize= " the"/>            </StackPanel>        </StackPanel>    </Grid>
New Person {     "Tom",      false= p;

This way we can display the different data types through a certain transformation in the program Interface!

Two methods of implementing data type Conversions in XAML in WPF

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.