Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3426816
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:45:41+00:00 2026-05-18T06:45:41+00:00

I have a simple ComboBox that has some simple values. I’m trying to do

  • 0

I have a simple ComboBox that has some simple values. I’m trying to do 2 way binding with an enum property on my model.

<ComboBox d:LayoutOverrides="Height" Grid.Column="1" SelectedItem="{Binding SortType, Converter={StaticResource sortSelect}, Mode=TwoWay}">
      <ListBoxItem Content="Ascending" Tag="Ascending"/>
      <ListBoxItem Content="Descending" Tag="Descending"/>
      <ListBoxItem Content="Absolute Ascending" Tag="AbsoluteAscending"/>
      <ListBoxItem Content="Absolute Descending" Tag="AbsoluteDescending" />
    </ComboBox>

Here is my ValueConverter

public class RdiSortMatchConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var val = (RdiSort) value;
        switch (val)
        {
            case RdiSort.Ascending:
                return "Ascending";
            case RdiSort.Descending:
                return "Descending";
            case RdiSort.AbsoluteAscending:
                return "Absolute Ascending";
            case RdiSort.AbsoluteDescending:
                return "Absolute Descending";
            default:
                throw new ArgumentOutOfRangeException();
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (RdiSort) Enum.Parse(typeof (RdiSort), (string) ((ListBoxItem) value).Tag);
    }
}

The ConvertBack method works fine, and updates my model based on the Tag value in the ListBoxItem, but I cant get the initial Enum value to select the correct ListBoxItem

whats the best way about achieving this, or is there a better way of binding t Enums (take into consideration that I need custom descriptions for each Enum value.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:45:41+00:00Added an answer on May 18, 2026 at 6:45 am

    You can do it like this. First add a description for each of your Enum values

    public enum RdiSort
    { 
        [Description("Ascending Description")] 
        Ascending, 
        [Description("Descending Description")] 
        Descending, 
        [Description("AbsoluteAscending Description")] 
        AbsoluteAscending,
        [Description("AbsoluteDescending Description")] 
        AbsoluteDescending
    } 
    

    Then use an ObjectDataProvider for your ComboBox

    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:MyEnumerations="clr-namespace:MyEnumerations" 
    
    <ObjectDataProvider MethodName="GetValues" 
                        ObjectType="{x:Type sys:Enum}" 
                        x:Key="RdiSortValues"> 
        <ObjectDataProvider.MethodParameters> 
            <x:Type TypeName="MyEnumerations:RdiSort" /> 
        </ObjectDataProvider.MethodParameters> 
    </ObjectDataProvider> 
    

    Use the RdiSortValues provider in your ComboBox and create a DataTemplate with a TextBlock and a Converter to see the Description instead of the Enum value.

    <local:EnumDescriptionConverter x:Key="EnumDescriptionConverter"/>
    
    <ComboBox SelectedItem="{Binding SortType}"
              ItemsSource="{Binding Source={StaticResource RdiSortValues}}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    

    And finally the converter. There is no need to ConvertBack since the converter is only used in the TextBlock for displaying.

    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;
            }
        }
    
        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                RdiSort myEnum = (RdiSort)value;
                string description = GetEnumDescription(myEnum);
                return description;
            }
            return null;
        }
        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return string.Empty;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some simple example WinForms code which I am trying to translate into
Should simple JavaBeans that have only simple getters and setters be unit tested?? What
I have a simple webform that will allow unauthenticated users to input their information,
I have a simple 2-column layout with a footer that clears both the right
I have a simple little test app written in Flex 3 (MXML and some
I have an array of objects that I'm trying to add to the Items
I have a ComboBox that is bound to an ObservableCollection of custom UserControls. Each
I have a WPF application using MVVM. I have some user controls that show
I have a simple class that provides state codes like this: public class StateProvider
I have a style for a ComboBox that I've been using for awhile, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.