Enum binding itemssource in WPF

Source: Internet
Author: User
ArticleDirectory
    • 3. Obtain the attribute

In WPF, enumerate and bind to itemssource.

1. Get the enum data source through objectdataprovider

First, we define an Enum class:

 
 Public EnumTableselectedtype
 
{
 
Selectedone,
 
 
 
Selectedtwo,
 
 
 
Selectedthird
 
}

Then define the data source in resource in XAML.

 
<Usercontrol. Resources>
 
<Objectdataprovider X: Key ="ODP"Methodname ="Getnames"Objecttype ="{X: Type System: Enum }">
<Objectdataprovider. methodparameters>
 
<X: Type typename ="Local: tableselectedtype"/>
 
</Objectdataprovider. methodparameters>
 
</Objectdataprovider>
 
</Usercontrol. Resources>

Here we have written an Enum data source whose key is ODP. Bind it to the itemssource of ComboBox.

 
<ComboBox itemssource ="{Binding source = {staticresource ODP }}"/>

:

However, sometimes we want to bind Enum, but want to display the corresponding Chinese string. For example, "selectone" is displayed as "first ".

Here I used converter ).

 Code  Public  Object Convert ( Object   Value , Type targettype, Object Parameter, cultureinfo culture ){ String [] Enmus = Value   As   String []; List < String > Result = New List < String > (); Foreach (VAR itemIn Enmus ){ Switch (Item ){ Case " Selectedone ": Result. Add (" First line "); Break ; Case " Selectedtwo ": Result. Add (" Row 2 "); Break ; Case " Selectedthird ": Result. Add (" Row 3 "); Break ;}} Return Result ;}

In XAML:

 
Code<ComboBox itemssource ="{Binding source = {staticresource ODP}, converter = {staticresource enumtypetostringconverter }}"/>

Effect:

2. Store Enum through dictionary and bind it to itemssource

This is convenient and efficient. We still use the above Enum type. We declare the tableselectedtypecollection attribute of a dictionary and initialize it.

 Public Partial ClassConboboxenum: usercontrol
 
{
 
PublicConboboxenum ()
 
{
 
Initializecomponent ();
 
 
 
Tableselectedtypecollection =NewDictionary <tableselectedtype,String> ();
 
Tableselectedtypecollection. Add (tableselectedtype. selectedone,"Article 1");
 
Tableselectedtypecollection. Add (tableselectedtype. selectedtwo,"Article 2");
Tableselectedtypecollection. Add (tableselectedtype. selectedthird,"Article 3");
 
This. Datacontext =This;
 
}
 
 
 
PublicDictionary <tableselectedtype,String> Tableselectedtypecollection {Get; set ;}
 
}

In XAML, we use tableselectedtypecollection to bind the itemssource of ComboBox.

 
<ComboBox itemssource ="{Binding tableselectedtypecollection }"Selectedvalue ="{Binding tableselectedtype }"Displaymemberpath ="Value"/>

Here we use value to display the corresponding Chinese text. Selectedvalue to bind its Enum type. Because the type in Enum is usually used in the background.

The effect is the same.

3. Obtain the attribute

The using system. componentmodel In the MS namespace is used here; the description feature is added to the enumeration element.

 
 Public EnumTableselectedtype {[description ("Select the first line")] Selectedone, [description ("Select the second line")] Selectedtwo, [description ("Select the third line")] Selectedthird}

Let's write an enumhelper to get it.

  Public   Static   Class Enumhelper { Public   Static T getenumattribute <t> (Enum source) Where T: attribute {type = source. GetType (); var sourcename = enum. getname (type, source); fieldinfo field = type. getfield (sourcename ); Object [] Attributes = field. getcustomattributes ( Typeof (T ), False ); Foreach (VAR o In Attributes ){ If (O Is T) Return (T) o ;} Return  Null ;} Public   Static   String Getdescription (Enum source) {var STR = getenumattribute <descriptionattribute> (source ); If (STR = Null ) Return   Null ; Return Str. description ;}}

Then we can call this enumeration when instantiating it.

  Public  dictionary 
  
    string  tableselectedtypedictionary {Get; set;} 
    Public  conboboxenum () {initializecomponent (); tableselectedtypedictionary = 
    New  dictionary 
   
     string  (); tableselectedtypedictionary. add (tableselectedtype. selectedone, enumhelper. getdescription (tableselectedtype. selectedone); tableselectedtypedictionary. add (tableselectedtype. selectedtwo, enumhelper. getdescription (tableselectedtype. selectedtwo); tableselectedtypedictionary. add (tableselectedtype. selectedthird, enumhelper. getdescription (tableselectedtype. selectedthird); 
     This . datacontext = 
     This ;}
   
  

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.