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

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

博客园 - 飞扬跋扈

用户中心 - 博客园 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;

            }