展会枚举说明,而不是名称 [英] Show Enum Description Instead of Name
问题描述
我已经绑定设置是这样的:
的ItemsSource ={绑定源= {我:枚举{X:类型信用:OccupationCategory}}}
的DisplayMemberPath =说明
的SelectedValue ={结合EmplType}
SelectedValuePath =值/>
和它的工作非常出色。不要在大型软件设计,我可以不再有任何产生INotifyPropertyChanged的事件,以便类型的数据绑定不起作用的变化。相反,我手动设置的selectedIndex和建设从code选项是这样的:
的ItemsSource ={绑定源= {StaticResource的ResidenceOwnershipType}}/>
该引用
< UserControl.Resources>
< ObjectDataProvider的X:键=ResidenceOwnershipType方法名=的GetValues对象类型={X:类型系统:枚举}>
< ObjectDataProvider.MethodParameters>
< X:类型类型名=信用:ResidenceOwnershipType/>
< /ObjectDataProvider.MethodParameters>
< / ObjectDataProvider的>
< /UserControl.Resources>
这工作尽可能的列表选项建设是关心,我的数据的链接,但我不能让组合框来显示枚举,而不是实际的文字说明标签。
我已经试过这样的事情:
的DisplayMemberPath =说明
但是这是不正确的。我将如何去这样做呢?
编辑:
我的枚举:
[DataContract]
公共枚举ResidenceOwnershipType
{
[描述()]
无= 0,
[说明(拥有家庭夺标)]
OwnsHomeOutright = 1,
[说明(购买家)
BuyingHome = 2,
[说明(出租/租赁)] //这里奇怪的顺序反映RouteOne公司网站
RentingLeasing = 4,
[说明(生活W /亲属)]
LivingWithRelatives = 3,
[说明(拥有/购买移动主页)]
MobileHome = 5,
[说明(未知)]
未知= 6
}
如果你把这个的ItemsSource
你必须定义一个定制的<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate.aspx\"><$c$c>ItemTemplate$c$c>作为的DisplayMemberPath
就是你将无法取回描述经由该路径。
至于模板应该是什么样子:你可以绑定一个的TextBlock
来枚举值(当前的的DataContext
)和管道将通过<一href=\"http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx\"><$c$c>ValueConverter$c$c>使用 Binding.Converter
。在code也只是一些反思检索说明
(的GetType
, GetCustomAttributes
等)
替代品是一个自定义的方法,该方法返回一个使用的收集马上(在使用的ObjectDataProvider
)或自定义的markup扩展它做同样的事情。
方法例如,如果我们是在谈论一个 ComponentModel.DescriptionAttribute
:
公共静态类EnumUtility
{
//可能要返回一个命名的类型,这是一个懒惰的例子(它的工作,虽然)
公共静态对象[] GetValuesAndDescriptions(类型enumType)
{
VAR值= Enum.GetValues(enumType).Cast&LT;对象&gt;();
从数值值VAR valuesAndDescriptions =
新选择
{
值=价值,
说明= value.GetType()
.GetMember(value.ToString())[0]
.GetCustomAttributes(真)
.OfType&LT; DescriptionAttribute&GT;()
。第一()
。描述
};
返回valuesAndDescriptions.ToArray();
}
}
&LT; ObjectDataProvider的X:键=数据方法名=GetValuesAndDescriptions
对象类型=本地:EnumUtility&GT;
&LT; ObjectDataProvider.MethodParameters&GT;
&LT; X:TypeExtension类型名=本地:TestEnum/&GT;
&LT; /ObjectDataProvider.MethodParameters>
&LT; / ObjectDataProvider的&GT;
&LT; ListBox中的ItemsSource ={绑定源= {StaticResource的数据}}
的DisplayMemberPath =说明
SelectedValuePath =值/&GT;
I had databinding set up like this:
ItemsSource="{Binding Source={my:Enumeration {x:Type credit:OccupationCategory}}}"
DisplayMemberPath="Description"
SelectedValue="{Binding EmplType}"
SelectedValuePath="Value"/>
and it worked really well. Do to a change in the larger software design I can no longer have anything that generates an INotifyPropertyChanged Event so that type of databinding doesn't work. Instead I am manually setting the selectedIndex and building the options from code like this:
ItemsSource="{Binding Source={StaticResource ResidenceOwnershipType}}"/>
which references
<UserControl.Resources>
<ObjectDataProvider x:Key="ResidenceOwnershipType" MethodName="GetValues" ObjectType="{x:Type System:Enum}" >
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="credit:ResidenceOwnershipType" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</UserControl.Resources>
That works as far as the building of the list options is concerned and the linking of all of my data, but I can't get the comboboxes to show the description tag in the enumeration instead of the actual text.
I've tried something like this:
DisplayMemberPath="Description"
but that wasn't correct. How would I go about doing this?
EDIT:
My Enum:
[DataContract]
public enum ResidenceOwnershipType
{
[Description("")]
None = 0,
[Description("Owns Home Outright")]
OwnsHomeOutright = 1,
[Description("Buying Home")]
BuyingHome = 2,
[Description("Renting/Leasing")] //Weird order here reflects RouteOne website
RentingLeasing = 4,
[Description("Living w/Relatives")]
LivingWithRelatives = 3,
[Description("Owns/Buying Mobile Home")]
MobileHome = 5,
[Description("Unknown")]
Unknown = 6
}
If you keep this ItemsSource
you will have to define a custom ItemTemplate
as the DisplayMemberPath
is just a path via which you will not be able to retrieve the description.
As for what the template should look like: You can bind a TextBlock
to the enum value (the current DataContext
) and pipe that through a ValueConverter
using Binding.Converter
. The code would just be some reflection to retrieve the Description
(GetType
, GetCustomAttributes
etc.)
Alternatives are a custom method that return a usable collection right away (and is used in the ObjectDataProvider
) or a custom markup extension which does the same thing.
Method example if we are talking about a ComponentModel.DescriptionAttribute
:
public static class EnumUtility
{
// Might want to return a named type, this is a lazy example (which does work though)
public static object[] GetValuesAndDescriptions(Type enumType)
{
var values = Enum.GetValues(enumType).Cast<object>();
var valuesAndDescriptions = from value in values
select new
{
Value = value,
Description = value.GetType()
.GetMember(value.ToString())[0]
.GetCustomAttributes(true)
.OfType<DescriptionAttribute>()
.First()
.Description
};
return valuesAndDescriptions.ToArray();
}
}
<ObjectDataProvider x:Key="Data" MethodName="GetValuesAndDescriptions"
ObjectType="local:EnumUtility">
<ObjectDataProvider.MethodParameters>
<x:TypeExtension TypeName="local:TestEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ListBox ItemsSource="{Binding Source={StaticResource Data}}"
DisplayMemberPath="Description"
SelectedValuePath="Value"/>
这篇关于展会枚举说明,而不是名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!