wpf ComboBox 绑定到枚举,我做错了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/878356/
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
ComboBox binding to enum, what did I do wrong?
提问by Angela
I have searched around and it seems very easy to bind enums to combobox, just retrieve Enum values as a list of strings via an ObjectDataProvider from the static Enum.GetValues method, however i can't get it to work. The error is Type ContactExportType was not found.
我四处搜索,似乎很容易将枚举绑定到组合框,只需通过静态 Enum.GetValues 方法中的 ObjectDataProvider 将枚举值作为字符串列表检索,但是我无法让它工作。错误是未找到类型 ContactExportType。
I have an enum called ContactExportType, it resides on Enums class. This class is part of the CEM.Marketing.Objects namespace.
我有一个名为 ContactExportType 的枚举,它驻留在 Enums 类中。此类是 CEM.Marketing.Objects 命名空间的一部分。
This is what i have:
这就是我所拥有的:
<UserControl
xmlns:local="clr-namespace:CEM.Marketing.Objects"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Grid>
<Grid.Resources>
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="ContactExportTypes">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:ContactExportType" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Grid.Resources>
</Grid>
<ComboBox
ItemsSource="{Binding {StaticResource ContactExportTypes}}"
...
Thanks, Angela
谢谢,安吉拉
回答by Thomas Levesque
To access a nested type, you should use the "+" separator :
要访问嵌套类型,您应该使用“+”分隔符:
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="ContactExportTypes">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:Enums+ContactExportType" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
By the way, there is a simpler way to bind to the values of an enum, without using an ObjectDataProvider. It's based on a custom markup extension :
顺便说一下,有一种更简单的方法可以绑定到枚举的值,而无需使用 ObjectDataProvider。它基于自定义标记扩展:
<ComboBox ItemsSource="{local:EnumValues local:Enums+ContactExportType}"/>
Here is the code for the EnumValues markup extension :
这是 EnumValues 标记扩展的代码:
[MarkupExtensionReturnType(typeof(object[]))]
public class EnumValuesExtension : MarkupExtension
{
public EnumValuesExtension()
{
}
public EnumValuesExtension(Type enumType)
{
this.EnumType = enumType;
}
[ConstructorArgument("enumType")]
public Type EnumType { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (this.EnumType == null)
throw new ArgumentException("The enum type is not set");
return Enum.GetValues(this.EnumType);
}
}
回答by Muad'Dib
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type local:Enums}"
x:Key="ContactExportTypes">
should be
应该
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="ContactExportTypes">
and
和
<x:Type TypeName="local:ContactExportType" />
should be
应该
<x:Type TypeName="CEM.Marketing.Objects.ContactExportType"/>
the sys:Enum points to the Enum framework class the typename in the parameter points to your fully qualified type-name.
sys:Enum 指向 Enum 框架类,参数中的 typename 指向您的完全限定类型名。
check Bea Stollnitz's blog
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="odp">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="namespace.class.TShirtSizes"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ComboBox ItemsSource="{Binding Source={StaticResource odp}}" IsSynchronizedWithCurrentItem="true"/>