Click here to Skip to main content
14,303,095 members

Fill Combobox With Sorted Enum Without Code

Rate this:
3.07 (7 votes)
Please Sign up or sign in to vote.
3.07 (7 votes)
19 Jun 2007CPOL
This article demonstrates how to bind a combobox with sorted names from an enum. This can all be accomplished in XAML without code in the XAML code behind page.


This article demonstrates how to bind a combobox with sorted names from an enum. This can all be accomplished in XAML without code in the XAML code behind page.


I'm in the middle of preparing for a Code Camp presentation this weekend and was reviewing all my code. I noticed an opportunity to refactor some code and then got the idea to just loosen the code and data bind using XAML.

I had previously read Beatriz Costa's great article, How can I bind an ItemsControl to the values of an enumeration? so I knew binding to the names of an enum was real simple.

Question was, how can I sort the names, keep my simple data binding "AND make the code reusable"?


Below is an Enum that has been declared. The order of the members in this enum is not friendly to a user using a combobox. We want to use the Enum, but display the member names sorted alphabetically.

Public Enum ZooCreatures
End Enum

Reusable Helper Function

We first need to crank up a reusable helper function that will take an enum type and return a sorted string array that we can use as an ItemsSource for our comboboxes.

For this really simple demo, I've just added the class shown below to the project. Normally, you would put this in your own helper class or help class project where you put all your other helper functions.

Public Class Helpers

    Public Function GetSortedEnumNames(ByVal t As Type) As String()

        'first do a sanity check, make sure the developer 
        'is passing us an enum
        If t.BaseType.FullName = "System.Enum" Then
            Dim strOut() As String = [Enum].GetNames(t)
            Return strOut
            Throw New ArgumentException("Must be an enum", "t")
        End If
    End Function

    Public Sub New()

    End Sub

End Class

How Does It Work

  • Sanity Check: use the BaseType.FullName property of the passed in type to ensure that the calling code actually passed in an Enum, if not, Throw New ArgumentException.
  • Get An Array Of Names From The Enum : use the GetNames shared (static for the C#'ers) method to return an unsorted list of the names in the Enum type. Actually, the names are returned in the order that the Enum type is declared.
  • Sort Array : use the Array.Sort shared method to sort the string array of names.
  • Return Sorted Names Array : how easy is this? {remember the old days?}

Let's Do Some Binding!

Here is our very simple, no code WPF Databinding in action. Entire XAML file:









    Title="Fill Combobox With Sorted Enum"



        <ObjectDataProvider x:Key="zooCreatureHelpersDS" 

        d:IsDataSource="True" MethodName="GetSortedEnumNames" 

        ObjectType="{x:Type local:Helpers}">
                <x:Type TypeName="local:ZooCreatures"/>

        <ComboBox x:Name="cboZooCreatures" 

        IsSynchronizedWithCurrentItem="True" Width="Auto" 

        ItemsSource="{Binding Mode=OneWay, 
        Source={StaticResource zooCreatureHelpersDS}}"/>

Breaking It Down


  • Really important to get your namespaces correct and you'll need them all or else you won't be compiling your project any time soon.
  • Need to set up an ObjectDataProvider for each Enum type that you want to data bind to. In our project we only have one combobox that needs to data bind to an Enum, so we'll just set up one ObjectDataProvider.
  • The ObjectDataProvider resource needs a key so that we can reference this resource in the XAML code. In this case, I named the key zooCreatureHelpersDS.
  • The MethodName="GetSortedEnumNames" property instructs the ObjectDataProvider resource to use the GetSortedEnumNames method. This is the name of our above helper method.
  • The ObjectType="{x:Type local:Helpers}" property instructs the ObjectDataProvider resource to use the class Helpers from the name space, FillComboboxWithSortedEnum. Notice how "local" is mapped to our class, FillComboboxWithSortedEnum.
  • Now we need to pass in the correct parameter to the GetSortedEnumNames method. This is easily accomplished using the <ObjectDataProvider.MethodParameters>
  • The <x:Type TypeName="local:ZooCreatures"/> property instructs the <ObjectDataProvider.MethodParameters> resource to pass the Type ZooCreatures to our helper.


  • Utilizing our new sorted array of Enum element names is super easy in XAML!
  • The ItemsSource="{Binding Mode=OneWay, Source={StaticResource zooCreatureHelpersDS}}" property of our combobox is bound to the StaticResource zooCreatureHelpersDS we just created.


  • 19 June 2007 : Initial Release


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Karl Shifflett
United States United States

I’m a passionate Software Architect and Developer.

A long-time WPF-Prism fanatic who enjoys writing line of business applications, developer tools, and Visual Studio Extensions.

My front end passions are WPF, Xamarin.Forms, Electron, ES2015 (ES6), Node.js, WordPress, and Angular.

For the back end I use what is appropriate for the project: SQL Server and ASP.NET WebAPI, MongoDB, Express, Azure, AWS, Firebase, etc.

I am very pragmatic software engineer and strive to write simple, maintainable, and testable code. Simple code allows for solving complex problems in a maintainable way.

My Blog

My Github Repros

Just a grain of sand on the worlds beaches.

Comments and Discussions

QuestionWhat about localization? Pin
Axel Rietschin20-Jun-07 10:00
professionalAxel Rietschin20-Jun-07 10:00 
AnswerRe: What about localization? Pin
Karl Shifflett20-Jun-07 10:59
memberKarl Shifflett20-Jun-07 10:59 
GeneralPossible improvement Pin
Josh Smith20-Jun-07 2:15
memberJosh Smith20-Jun-07 2:15 
GeneralRe: Possible improvement Pin
Karl Shifflett20-Jun-07 2:30
memberKarl Shifflett20-Jun-07 2:30 
GeneralRe: Possible improvement Pin
Josh Smith20-Jun-07 2:35
memberJosh Smith20-Jun-07 2:35 
GeneralRe: Possible improvement Pin
Josh Smith20-Jun-07 3:34
memberJosh Smith20-Jun-07 3:34 
GeneralRe: Possible improvement Pin
Karl Shifflett20-Jun-07 5:02
memberKarl Shifflett20-Jun-07 5:02 
QuestionWhat's does xaml bring me? Pin
wout de zeeuw19-Jun-07 22:30
memberwout de zeeuw19-Jun-07 22:30 
AnswerRe: What's does xaml bring me? Pin
Karl Shifflett20-Jun-07 0:35
memberKarl Shifflett20-Jun-07 0:35 
QuestionWithout code? Pin
SimesA19-Jun-07 21:36
memberSimesA19-Jun-07 21:36 
AnswerRe: Without code? Pin
Karl Shifflett20-Jun-07 0:38
memberKarl Shifflett20-Jun-07 0:38 
GeneralMy thoughts Pin
Moim Hossain19-Jun-07 20:07
memberMoim Hossain19-Jun-07 20:07 
GeneralRe: My thoughts Pin
Karl Shifflett20-Jun-07 0:49
memberKarl Shifflett20-Jun-07 0:49 
GeneralSuggestion Pin
mav.northwind19-Jun-07 19:32
membermav.northwind19-Jun-07 19:32 
GeneralRe: Suggestion Pin
Karl Shifflett20-Jun-07 1:04
memberKarl Shifflett20-Jun-07 1:04 
GeneralRe: Suggestion Pin
Bohicette20-Jun-07 9:39
memberBohicette20-Jun-07 9:39 
GeneralRe: Suggestion Pin
Karl Shifflett20-Jun-07 12:29
memberKarl Shifflett20-Jun-07 12:29 
GeneralRe: Suggestion Pin
Neal Andrews8-Jul-07 7:01
memberNeal Andrews8-Jul-07 7:01 
GeneralRe: Suggestion Pin
Karl Shifflett8-Jul-07 7:05
memberKarl Shifflett8-Jul-07 7:05 
GeneralRe: Suggestion Pin
Karl Shifflett20-Jun-07 1:49
memberKarl Shifflett20-Jun-07 1:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 19 Jun 2007


23 bookmarked