WPF-displaying enums in ComboBox control

Source: Internet
Author: User

«Hiding from debugger | main | WPF: Gadget-style User Experience»

WPF-displaying enums in ComboBox control

Displaying enums in ComboBox in WPF isn't really rocket science. the issue however comes when you want to display user friendly strings against the enum values. enum unfortunately doesn't support this by a simple override to the tostring method.

The usual approach people take is the use the descriptionattribute. this attribute is assigned to each Enum value and at runtime, using reflection, the value of the attribute is queried and is then displayed. the approach is definitely worth considering and I found an implementation of the same and a custom WPF ComboBox that will display such user friendly Enum strings here.

However reflection does mean a bit of performance impact and depending on the size of the enum, this can vary. in the above implementation, the values are hence cached for any reuse and improve efficiency. however if the binding happens only once, this has no effect.

I was hence wondering on how to do this without having to use reflection. I then hit upon an idea of using a dictionary object with the enum as the key and the value as the user friendly string. it was then a matter of binding this with the ComboBox and set the displaymemberpath appropriately. in order to ensure that the Enum and this dictionary collection go hand in hand, I decided to encapsulate them in a single class. there is definitely a maintenance overhead of ensuring that appropriate key value pair is added to the dictionary for each enum. see the code for this class below.

Internal class Utility

{

Static utility ()

{

// Initialize the collection with user friendly strings for each Enum

_ Monthcol = new dictionary <months, string> (){

{Months. Jan, "January "},

{Months. Feb, "February "},

{Months. Mar, "March "},

{Months. Apr, "yml "},

{Months. May, "may "},

{Months. Jun, "June "},

{Months. Jul, "July "},

{Months. Aug, "August "},

{Months. SEP, "September "},

{Months. Oct, "October "},

{Months. Nov, "November "},

{Months. Dec, "December "}};

 

}

 

Public Enum months

{

Jan,

Feb,

Mar,

Apr,

May,

Jun,

Jul,

Aug,

SEP,

Oct,

Nov,

Dec

}

 

Private Static dictionary <months, string> _ monthcol;

Public static dictionary <months, string> monthcollection

{

Get

{

Return _ monthcol;

}

}

}

And in XAML, this utility class can be instantiated as a resource and then referenced in the ComboBox creation.

<ComboBox itemssource = "{binding source = {staticresource monthcollection}, Path = monthcollection}" selectedindex = "0" displaymemberpath = "value" selectedvaluepath = "key"/>

Note that the selectedvaluepath property has also been set to point to the key in the dictionary. with this, it is easy to handle the selection changed event and the ComboBox's selectedvalue property will then be the enum value and you can then easily use a switch/case block to handle it appropriately. if you don't do it, you can still easily cast this To Get To The enum value as below

Switch (keyvaluepair <utility. Months, string>) cmbbox. selectedvalue). Key)

{

// Your case statements here

Default:

// Custom Logic

Break;

}

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.