显示枚举描述而不是名称 [英] Show Enum Description Instead of Name
问题描述
我的数据绑定设置如下:
I had databinding set up like this:
ItemsSource="{Binding Source={my:Enumeration {x:Type credit:OccupationCategory}}}"
DisplayMemberPath="Description"
SelectedValue="{Binding EmplType}"
SelectedValuePath="Value"/>
而且效果非常好.对较大的软件设计进行更改 我再也无法生成 INotifyPropertyChanged 事件,因此该类型的数据绑定不起作用.相反,我手动设置 selectedIndex 并从如下代码构建选项:
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}}"/>
哪些引用
<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.
我尝试过这样的事情:
DisplayMemberPath="Description"
但那是不正确的.我该怎么做呢?
but that wasn't correct. How would I go about doing this?
我的枚举:
[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
}
推荐答案
如果你保留这个 ItemsSource
你将不得不定义一个自定义的 ItemTemplate
因为 DisplayMemberPath
只是一个路径,您将通过它无法检索说明.
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.
至于模板应该是什么样子:您可以将 TextBlock
绑定到枚举值(当前的 DataContext
)并通过 ValueConverter
使用 Binding.Converter
.代码只是一些反射来检索 Description
(GetType
、GetCustomAttributes
等)
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.)
替代方法是一种立即返回可用集合的自定义方法(并在 ObjectDataProvider
中使用)或自定义 标记扩展 做同样的事情.
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.
如果我们谈论的是 ComponentModel.DescriptionAttribute
的方法示例:
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屋!