简体   繁体   中英

Binding combobox with an Enumeration value and get the selected item in the form of enum

I have an enumeration:

public enum InspectionCardsGroupOption
   {
      Application = 0,
      ApplicationType,
      InspectionStation,
      ComponentSide
   };

Now I am using wpf MVVM design pattern. This enumeration is in ViewModel. I have a ComboBox in xaml. I need to bind that ComboBox to this enum and on selection change of ComboBox it should give me the enum value in the form of enum value.

platform: windows10 , language :C# I am new to programming so if anyone can give me elaborate explanation, then it will be helpful for me.

First create a ObjectDataProvider in xaml as follows inside Resource tag :

<ObjectDataProvider  x:Key="odpEnum"
                         MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:Comparators"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    <local:EnumDescriptionConverter x:Key="enumDescriptionConverter"></local:EnumDescriptionConverter>

Now in the above code sample the sys:Enum is having an alias sys which is coming from namespace xmlns:sys="clr-namespace:System;assembly=mscorlib". So this needs to be added.

Add a combobox as follows :

<ComboBox Grid.Row="1" Grid.Column="1"  Height="25" Width="100" Margin="5" 
          ItemsSource="{Binding Source={StaticResource odpEnum}}"
          SelectedItem="{Binding Path=MySelectedItem}"
          >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Converter={StaticResource enumDescriptionConverter}}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

Let me consider you have a enum type as follows :

public enum Comparators
{
    [Description("Application")]
    Application,
    [Description("Application Type")]
    ApplicationType,
    [Description("Inspection Name")]
    InspectionName,
    [Description("Component Type")]
    ComponentType
}

so keep it in viewmodel section (outside the view model but inside the same namespace as that of viewmodel)

Now create a property in viewmodel as follows to get the selectedItem from XAML ComboBox

private Comparators _MySelectedItem;
    public Comparators MySelectedItem
    {
        get { return _MySelectedItem; }
        set
        {
            _MySelectedItem = value;
            OnPropertyChanged("MySelectedItem");
        }
    }

Create a converter class as follows :--

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

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {            
        Enum myEnum = (Comparators)value;            
        string description = GetEnumDescription(myEnum);
        return description;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return string.Empty;
    }
}

when you will run this sample then you will find you will get selectedItem as of type enumeration in _MySelectedItem = value; in the property of the viewmodel.

This can be achieved in an MVVM pure manner, with a couple of helper classes.

myValueDisplayPair.cs

/// <summary>
/// Equivalent to KeyValuePair<object, string> but with more memorable property names for use with ComboBox controls
/// </summary>
/// <remarks>
/// Bind ItemsSource to IEnumerable<ValueDisplayPair>, set DisplayMemberPath = Display, SelectedValuePath = Value, bind to SelectedValue
/// </remarks>
public abstract class myValueDisplayPair
{
    public object Value { get; protected set; }
    public string Display { get; protected set; }
}

/// <summary>
/// Equivalent to KeyValuePair<T, string>
/// </summary>
/// <typeparam name="T"></typeparam>
public class myValueDisplayPair<T> : myValueDisplayPair
{
    internal perValueDisplayPair(T value, string display)
    {
        Value = value;
        Display = display;
    }

    public new T Value { get; }

    public override string ToString() => $"Display: {Display} Value: {Value}";
}

myEnumHelper.cs

/// <summary>
/// Helper class for enum types 
/// </summary>
public static class myEnumExtender
{
    /// <summary>
    /// Get the value of a Description attribute assigned to an enum element 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string Description(this Enum value)
    {
        var fieldInfo = value
            .GetType()
            .GetField(value.ToString());

        var attributes = fieldInfo
            .GetCustomAttributes(typeof(DescriptionAttribute), false)
            .OfType<DescriptionAttribute>()
            .ToList();

        return attributes.Any() ? attributes.First().Description : value.ToString();
    }

    /// <summary>
    /// Gets all the elements of an enum type 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <remarks>
    /// c# doesn't support where T: Enum - this is the best compromise
    /// </remarks>
    public static ReadOnlyCollection<T> GetValues<T>() 
        where T : struct, IComparable, IFormattable, IConvertible
    {
        var itemType = typeof (T);

        if (!itemType.IsEnum)
            throw new ArgumentException($"Type '{itemType.Name}' is not an enum");

        var fields = itemType
            .GetFields()
            .Where(field => field.IsLiteral);

        return fields
            .Select(field => field.GetValue(itemType))
            .Cast<T>()
            .ToList()
            .AsReadOnly();
    }

    /// <summary>
    /// Generate a <see cref="myValueDisplayPair"/> list containing all elements of an enum type
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="sortByDisplay"></param>
    public static ReadOnlyCollection<myValueDisplayPair<T>> MakeValueDisplayPairs<T>(bool sortByDisplay = false) 
        where T : struct, IComparable, IFormattable, IConvertible
    {
        var itemType = typeof(T);

        if (!itemType.IsEnum)
            throw new ArgumentException($"Type '{itemType.Name}' is not an enum");

        var values = GetValues<T>();
        var result = values
            .Select(v => v.CreateValueDisplayPair())
            .ToList();

        if (sortByDisplay)
            result.Sort((p1, p2) => string.Compare(p1.Display, p2.Display, StringComparison.InvariantCultureIgnoreCase));

        return result.AsReadOnly();
    }
}

These can be used in your ViewModel

public ReadOnlyCollection<lcValueDisplayPair<InspectionCardsGroupOption>> AllInspectionCardsGroupOptions { get; } 
    = myEnumExtender.MakeValueDisplayPairs<InspectionCardsGroupOption>();

private InspectionCardsGroupOption _selectedInspectionCardsGroupOption;

public InspectionCardsGroupOption SelectedInspectionCardsGroupOption
{
    get => _selectedInspectionCardsGroupOption;
    set => Set(nameof(SelectedInspectionCardsGroupOption), ref _selectedInspectionCardsGroupOption, value)
}

and in your View

<ComboBox ItemsSource="{Binding AllInspectionCardsGroupOptions}"
          DisplayMemberPath="Display"
          SelectedValuePath="Value"
          SelectedValue="{Binding SelectedAllInspectionCardsGroupOptions, mode=TwoWay}" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM