Show enumeration name instead of name

I had data binding like this:

ItemsSource="{Binding Source={my:Enumeration {x:Type credit:OccupationCategory}}}"
                      DisplayMemberPath="Description"
                      SelectedValue="{Binding EmplType}"
                      SelectedValuePath="Value"/>

and it worked very well. Make a larger software change, I can no longer have anything that generates an INotifyPropertyChanged event so that the data binding type does not work. Instead, I manually install selectedIndex and build the parameters from the code as follows:

ItemsSource="{Binding Source={StaticResource ResidenceOwnershipType}}"/>

which refers to

<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>

This works in terms of creating list parameters and linking all my data, but I can't get comboboxes to show the description tag in an enumeration instead of the actual text.

I tried something like this:

DisplayMemberPath="Description"

but it was wrong. How should I do it?

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
}
+5
source share
2 answers

ItemsSource, ItemTemplate, DisplayMemberPath - , .

: TextBlock enum ( DataContext) , ValueConverter Binding.Converter. Description (GetType, GetCustomAttributes ..)

- , ( ObjectDataProvider) , .


, 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"/>
+11

H.B. , :

, Description:

Description = (value.GetType().GetMember(value.ToString())[0].GetCustomAttributes(true).OfType<DescriptionAttribute>().Count() > 0 ? 
                                                value.GetType().GetMember(value.ToString())[0].GetCustomAttributes(true).OfType<DescriptionAttribute>().First().Description 
                                                : value)

, , : SelectedValuePath="Value"

+1

All Articles