WPF How to bind an enum with descriptions to a combobox

拥有回忆 提交于 2019-12-05 18:31:04

问题


Hi I want to bind an enum with descriptions to a combobox:

I got next enum:

  public enum ReportTemplate
  {
     [Description("Top view")]
     1,
     [Description("Section view")]
     2
  }

I tried this:

  <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type System:Enum}"  
  x:Key="ReportTemplateEnum">
      <ObjectDataProvider.MethodParameters>
          <x:Type TypeName="Helpers:ReportTemplate" />
      </ObjectDataProvider.MethodParameters>
  </ObjectDataProvider>

  <Style x:Key="ReportTemplateCombobox" TargetType="dxe:ComboBoxEditSettings">
      <Setter Property="ItemsSource" 
      Value="{Binding Source={x:Type Helpers:ReportTemplate}}"/>
      <Setter Property="DisplayMember" Value="Description" />
      <Setter Property="ValueMember" Value="Value" />
  </Style>

Can't succeed to do this any 1 knows a simple solution?

Thanks in advance!


回答1:


This can be done by using a converter and item template for your comboBox.

Here is the converter code which when bound to an enum will return the Description value:

namespace FirmwareUpdate.UI.WPF.Common.Converters
{
    public class EnumDescriptionConverter : IValueConverter
    {
        private string GetEnumDescription(Enum enumObj)
        {
            FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());

            object[] attribArray = fieldInfo.GetCustomAttributes(false);

            if (attribArray.Length == 0)
            {
                return enumObj.ToString();
            }
            else
            {
                DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute;
                return attrib.Description;
            }
        }

        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Enum myEnum = (Enum)value;
            string description = GetEnumDescription(myEnum);
            return description;
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return string.Empty;
        }
    }
}

Then in your xaml you need to use and item template.

<ComboBox Grid.Row="1" Grid.Column="1"  Height="25" Width="100" Margin="5"
              ItemsSource="{Binding Path=MyEnums}"
              SelectedItem="{Binding Path=MySelectedItem}"
              >
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Converter={StaticResource enumDescriptionConverter}}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>



回答2:


RSmaller has a good answer, and is the one I use as well, with one caveat. If you have more than one attribute on your enums, and Description isn't the first listed then his "GetEnumDescription" method will throw an exception...

Here is a slightly safer version:

    private string GetEnumDescription(Enum enumObj)
    {
        FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());

        object[] attribArray = fieldInfo.GetCustomAttributes(false);

        if (attribArray.Length == 0)
        {
            return enumObj.ToString();
        }
        else
        {
            DescriptionAttribute attrib = null;

            foreach( var att in attribArray)
            {
                if (att is DescriptionAttribute)
                    attrib = att as DescriptionAttribute;
            }

            if (attrib != null )
                return attrib.Description;

            return enumObj.ToString();
        }
    }



回答3:


public enum ReportTemplate
{
 [Description("Top view")]
 Top_view=1,
 [Description("Section view")]
 Section_view=2
}

ComboBoxEditSettings.ItemsSource = EnumHelper.GetAllValuesAndDescriptions(new
List<ReportTemplate> { ReportTemplate.Top_view, ReportTemplate.Section_view });


来源:https://stackoverflow.com/questions/15567913/wpf-how-to-bind-an-enum-with-descriptions-to-a-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!