• WPF Displaying enums in ComboBox control


    « 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, "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;

                }

  • 相关阅读:
    Java实现Labeling Balls(拓扑排序的应用)
    Java实现Labeling Balls(拓扑排序的应用)
    Java实现Labeling Balls(拓扑排序的应用)
    Java实现Labeling Balls(拓扑排序的应用)
    string与QString转换(string既可以是utf8,也可以是gbk)
    Qt4.8.6详细安装步骤(使用了i686-4.8.2-release-posix-dwarf-rt_v3-rev3,手动设置gcc和gdb)非常清楚 good
    qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method
    小试X64 inline HOOK,hook explorer.exe--->CreateProcessInternalW监视进程创建
    RtlAdjustPrivilege进程提权,权限ID对照表
    jQuery AJAX 网页无刷新上传示例
  • 原文地址:https://www.cnblogs.com/rickiedu/p/2041894.html
Copyright © 2020-2023  润新知