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

  • SEARCH
  • Home
  • 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 6596347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:03:51+00:00 2026-05-25T18:03:51+00:00

My models use enums for fixed multiple selections. I’m using Dean Chalk’s EnumBinder, found

  • 0

My models use enums for fixed multiple selections. I’m using Dean Chalk’s EnumBinder, found at http://deanchalk.me.uk/post/Enumeration-Binding-In-Silverlight.aspx , to bind to a combo box. Everything seems to work great except the default value isn’t shown. The selected index is -1, and it doesn’t matter if I bind to SelectedItem or SelectedValue. The combobox works fine otherwise. I have no problems with default values with any other bound comboboxes.

enumbindingsupport.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

/* All of this was once just part of the RichClient sub-namespace
 * but it has its uses elsewhere, so it has been moved. 
 */

/// <summary>
/// Container for enumeration values.
/// </summary>
/// <remarks>
/// http://deanchalk.me.uk/post/Enumeration-Binding-In-Silverlight.aspx
/// </remarks>
public sealed class EnumContainer
{
    public int EnumValue { get; set; }
    public string EnumDescription { get; set; }
    public object EnumOriginalValue { get; set; }
    public override string ToString() {
        return EnumDescription;
    }

    public override bool Equals(object obj) {
        if (obj == null)
            return false;
        if (obj is EnumContainer)
            return EnumValue.Equals((int)((EnumContainer)obj).EnumValue);
        return EnumValue.Equals((int)obj);
    }

    public override int GetHashCode() {
        return EnumValue.GetHashCode();
    }

}

/// <summary>
/// A collection to store a list of EnumContainers that hold enum values.
/// </summary>
/// <remarks>
/// http://deanchalk.me.uk/post/Enumeration-Binding-In-Silverlight.aspx
/// </remarks>
/// <typeparam name="T"></typeparam>
public class EnumCollection<T> : List<EnumContainer> where T : struct
{
    public EnumCollection() {
        var type = typeof(T);
        if (!type.IsEnum)
            throw new ArgumentException("This class only supports Enum types");
        var fields = typeof(T).GetFields(BindingFlags.Static | BindingFlags.Public);
        foreach (var field in fields) {
            var container = new EnumContainer();
            container.EnumOriginalValue = field.GetValue(null);
            container.EnumValue = (int)field.GetValue(null);
            container.EnumDescription = field.Name;
            var atts = field.GetCustomAttributes(false);
            foreach (var att in atts)
                if (att is DescriptionAttribute) {
                    container.EnumDescription = ((DescriptionAttribute)att).Description;
                    break;
                }
            Add(container);
        }

    }
}

enumvalueconverter.cs

using System;
using System.Globalization;
using System.Windows.Data;

/// <summary>
/// Supports two-way binding of enumerations.
/// </summary>
public class EnumValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
                            CultureInfo culture) {
        return (int)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                            CultureInfo culture) {
        if (value == null)
            return null;
        if (value.GetType() == targetType)
            return value;
        return ((EnumContainer)value).EnumOriginalValue;
    }
}

enum used:

/// <summary>
/// Describes the available sorting options.
/// </summary>
public enum PeopleSortOptionsEnum
{

    [Display(Order = 10)]
    [Description("First Name, Last Name")]
    FirstNameThenLastName,

    [Display(Order = 20)]
    [Description("Last Name, First Name")]
    LastNameThenFirstName,

    [Display(Order = 30)]
    Grade,

    [Display(Order = 40)]
    Gender,

    [Display(Order = 50)]
    Age
}

property on my model:

    /// <summary>
    /// This is the list for the enumeration to bind to.
    /// </summary>
    public EnumCollection<PeopleSortOptionsEnum> AvailableSortOptions
    {
        get { return new EnumCollection<PeopleSortOptionsEnum>(); }
    }

XAML snippet:

    <ComboBox ItemsSource="{Binding Path=AvailableSortOptions, Mode=OneWay}" SelectedValue="{Binding Path=Preferences.SortOrder, Mode=TwoWay, Converter={StaticResource EnumConverter}}" Grid.Column="1" Grid.Row="2" Height="32" Margin="48,31,0,0" x:Name="cboSort" VerticalAlignment="Top" />

Where Preferences.SortOrder is of type PeopleSortOptionsEnum, the converter is in my app.xaml as a converter for all enum types.

Anyone have any idea why it won’t set the index to the currently selected value? I’m about to just throw some code in the codebehind to set the selectedindex to the currently selected value on load, but I feel so dirty just thinking about it.

Besides this issue, it works wonderfully, so thanks Dean!

Edit Adding the Preferences.SortOrder code:

public PeopleSortOptionsEnum SortOrder 
{ 
  get { return sortOrder; } 
  set 
  { 
    if (sortOrder != value) 
    { 
      sortOrder = value; 
      PropertyHasChanged("SortOrder"); 
    } 
  } 
}
  • 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-25T18:03:52+00:00Added an answer on May 25, 2026 at 6:03 pm

    The issue is the Convert method on your enum converter class:

    public object Convert(object value, Type targetType, object parameter,
                            CultureInfo culture) {
        return (int)value;
    }
    

    When the combobox SelectedItem is gets the value from SortOrder it is return in Enum value which this converter is converting to an int. However, the combobox items is a collection of EnumContainer objects, not ints. So it fails to set the selected item.

    To resolve the issue you have to do two things. First change your combobox bindings and set the SelectedValuePath:

    <ComboBox ItemsSource="{Binding Path=AvailableSortOptions}" 
              SelectedValue="{Binding Path=SortOrder, Mode=TwoWay, Converter={StaticResource EnumConverter}}" 
              SelectedValuePath="EnumOriginalValue"/>
    

    Second you have to slightly modify the Convert method on your converter:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      return value;
    }
    

    Once I made those two changes it started working as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create basic low-poly 3D models to use in XNA Game Studio
Can I have a custom service providing the storage of the models I use
I would like to use Markov models for some architecture simulations, but don't have
Should I use the same controller and view for editing and creating models in
I would like to use the django contrib.admin pages to edit my models, but
I'm looking for a simple file format to use for wireframe models. I am
What are the ways that you use to model and retrieve hierarchical info in
I am trying to adapt a simple WPF application to use the Model-View-ViewModel pattern.
We use a simple object model for our low level networking code at work
When I use the default model binding to bind form parameters to a complex

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.