惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
博客园 - 司徒正美
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
罗磊的独立博客
IT之家
IT之家
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
J
Java Code Geeks
L
LangChain Blog
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog

博客园 - 飞扬跋扈

用户中心 - 博客园 Using sql azure for Elmah Invalid object name ‘sys.configurations’. (Microsoft SQL Server, Error: 208) Cannot install ubuntu or other linux flavours on citrix Xen server ubuntu重置root密码 How to join a Ubuntu to Windows Domain 在ubuntu上搭建reviewboard jQuery设计思想 用户中心 - 博客园 节能的荧光胶带让你起夜不开灯 Outlook发信 ABAP TIPS (1) -- F4检索功能的实现 教菜鸟建个无敌文件夹 Don’t Delete – Just Don’t [python 3.1]删除指定目录下的bin,obj文件夹 用户中心 - 博客园 [转]Webbrowser控件判断网页加载完毕的简单方法 .py to exe Tabs versus Spaces
WPF - Displaying enums in ComboBox control
飞扬跋扈 · 2011-05-10 · via 博客园 - 飞扬跋扈

« Hiding from Debugger | Main | WPF: Gadget-Style User Experience »

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, "April"},

                {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;

            }