Binding a ComboBox to Enum Values with RadControls for Silverlight

Posted by: the telerik blogs, on 12 Jun 2009 | View original | Bookmarked: 0 time(s)

A common request in our support is to display a combo box, containing all values of a specific Enum. This is fairly easy with RadComboBox for Silverlight - all you need to do is to fill its ItemsSource collection with the Enum values. The best way to do this is to create a view model for the Enum:

using System;
using System.Collections;
using System.Linq;
using System.Reflection;

namespace SilverlightApplication1
{
    public class EnumModel
    {
        private Type enumType;

        public IEnumerable Values { get; private set; }

        [TypeConverter(typeof(TypeTypeConverter))]
        public Type EnumType
        {
            get
            {
                return this.enumType;
            }
            set
            {
                this.enumType = value;
                this.InitValues();
            }
        }

        private void InitValues()
        {
            this.Values = this.EnumType
                .GetFields(BindingFlags.Public | BindingFlags.Static)
                .Select<FieldInfo, object>(
                    (FieldInfo x) => x.GetValue(this.EnumType));
        }
    }
}

 

The trick here is the TypeConverter attribute on the EnumType property, that allows me to declare the EnumModel in XAML:

<UserControl.Resources> <local:EnumModel x:Key="EnumModel" 
 EnumType="System.Windows.Controls.ClickMode" /> </UserControl.Resources>

 

There are some limitations in this approach, though you should modify the TypeTypeConverter class so that it can recognize the assemblies that might contain the enumerations, defined in XAML. See the TypeTypeConverter.cs in the attached source code for more information. Of course, if you create the EnumModel in the code behind you will be able to provide any Enum Type you want in this case the modifications in the TypeTypeConverter class will not be necessary.

The rest is really simple:

<Grid DataContext="{StaticResource EnumModel}"> <telerikInput:RadComboBox ItemsSource="{Binding Values}" Width="100" Height="25" DisplayMemberPath="" /> </Grid>

 

Note the DisplayMemberPath attribute with empty string value it has to be present, in order the combo box to be able properly to display its selected item. If you remove it, the control will display the Enums integer value in its selection box, instead of its text representation.

Here is the source code of the application

EnumValuesInCombo

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: ASP.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 3019 | Hits: 3

Similar Posts

  • Telerik Launches RadControls for Silverlight 3 for Line-of-Business Application Development more
  • Dynamic Delegates with Expression Trees more
  • Using Enums in List Controls more
  • Moonlight 1.0 Released, Silverlight script updated – and a Chrome hack more
  • "Silverlight: Write and Win!" Contest more
  • Reflection, GetMember() and COM objects? more
  • How the WCF LOB Adapter SDK works with IChannelFactory and IChannelListener more
  • Multiple Enumeration of LINQ results more
  • More about WCF Binding configurations in STS scenarios more
  • Writing Clone-friendly Binding Elements more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD