wpf WPF如何将带有描述的枚举绑定到组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15567913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF How to bind an enum with Description to a ComboBox
提问by dg90
How can I bind an enumwith Description(DescriptionAttribute) to a ComboBox?
如何将enumwith Description( DescriptionAttribute)绑定到 a ComboBox?
I got an enum:
我得到了一个enum:
public enum ReportTemplate
{
[Description("Top view")]
TopView,
[Description("Section view")]
SectionView
}
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 simple solution?
无法成功做到这一点,任何简单的解决方案?
Thanks in advance!
提前致谢!
回答by RSmaller
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.
然后在您的 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>
回答by Brock
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...
RSmaller 有一个很好的答案,也是我使用的答案,但有一个警告。如果枚举上有多个属性,并且描述不是第一个列出的,那么他的“GetEnumDescription”方法将抛出异常......
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();
}
}
回答by user3809159
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 });

