WPF如何将带有描述的枚举绑定到组合框

dg9*_*g90 11 c# wpf combobox enumeration devexpress

嗨,我想将枚举与组合框的描述绑定在一起:

我得到了下一个枚举:

  public enum ReportTemplate
  {
     [Description("Top view")]
     1,
     [Description("Section view")]
     2
  }
Run Code Online (Sandbox Code Playgroud)

我试过这个:

  <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>
Run Code Online (Sandbox Code Playgroud)

不能成功做到这一点1知道一个简单的解决方案吗?

提前致谢!

RSm*_*ler 14

这可以通过使用comboBox的转换器和项模板来完成.

这是转换器代码,当绑定到枚举时将返回Description值:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的xaml中你需要使用和项目模板.

<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>
Run Code Online (Sandbox Code Playgroud)

  • 什么是MyEnums和MySelectedItem? (6认同)

小智 5

RSmaller 有一个很好的答案,也是我使用的答案,但有一个警告。如果枚举上有多个属性,并且描述不是第一个列出的,那么他的“GetEnumDescription”方法将抛出异常......

这是一个稍微安全的版本:

    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();
        }
    }
Run Code Online (Sandbox Code Playgroud)