Version

Handling Enum Data Type (xamComboEditor)

Topic Overview

Purpose

This topic explains the behavior of the control when bound to an enum data type.

Required background

The following topic is prerequisites to understanding this topic:

Topic Purpose

This topic will help you to better understand the functionalities of the control.

Enum Handling

Summary

When the xamComboEditor control has both its ItemsSource and ItemsProvider properties not set and the ValueType property set to an enum (or a nullable enum) type then an internal items provider is created and populates the dropdown with the values obtained from the enum type.

The enum values are extracted in the following manner:

  • it obtains a type converter with a call to TypeDescriptor.GetConverter(valueType)

  • it then enumerates over the converter’s GetStandardValues() collection which also determines the order of the values in the dropdown

The null value of a nullable enum is handled in the following way: if the converter converts the null to a non-empty string it will be used as the display value, otherwise a localized string with a key of Nullable_Enum_NullValue_Literal (which for English is "[null]") will be used. This resource is in the Editors assembly.

There are ways to exclude certain properties from the dropdown, change their display value or even change the order they are shown in the dropdown. All these features are described in the table below.

Configuration tasks

The following table explains briefly the configurable aspects of the enum handling feature of the xamComboEditor control.

Task Sample Attribute

Populate the control’s dropdown with enum’s values.

The following example demonstrates how to set an enum type to the ValueType property which will result in populating the control’s dropdown with the enum’s values.

In Visual Basic:

Public Enum Foods
  Fruits
  Vegetables
  Meat
  Dairy
  Bread
  Eggs
End Enum

In C#:

public enum Foods
{
  Fruits,
  Vegetables,
  Meat,
  Dairy,
  Bread,
  Eggs
}

In XAML:

...
<igEditors:XamComboEditor ValueType="{x:Type Foods}">
</igEditors:XamComboEditor>
...

ValueType

Exclude an enum value from the control’s dropdown.

The following example demonstrates the definition of an enum value "AnEnumValue" which will not be included in the dropdown list because it has its "Browsable" attribute set to false.

In Visual Basic:

Public Enum AnEnum
  <Browsable(False)> _
  AnEnumValue
End Enum

In C#:

public enum AnEnum
{
  [Browsable(false)]
  AnEnumValue
}
Note
Note

If the xamComboEditor’s IsEditable property is true the user will still be able to set an excluded value.

Browsable

Provide a custom description for an enum value.

The following example demonstrates the definition of a custom description (which will be shown in the dropdown) for an enum value instead of its name using the "Description" attribute.

In Visual Basic:

Public Enum DrinksEnum
  <Description("Apple Juice")> _
  JuiceApple
End Enum

In C#:

public enum DrinksEnum
{
  [Description("Apple Juice")]
  JuiceApple
}

Description

Customize the order of the enum values

The following example demonstrates the definition of an enum with several values and a custom type converter.

In Visual Basic:

<TypeConverter(GetType(MyTypeConverter))> _
Public Enum Foods
  Fruits
  Vegetables
  Meat
  Dairy
  Bread
  Eggs
End Enum

In C#:

[TypeConverter(typeof(MyTypeConverter))] public enum Foods
{
  Fruits,
  Vegetables,
  Meat,
  Dairy,
  Bread,
  Eggs
}

The following example demonstrates the definition of a type converter which provides a custom list ordering which will be used to populate the drop down.

In Visual Basic:

Public Class MyTypeConverter
  Inherits TypeConverter
  Public Overrides Function GetStandardValues(context As ITypeDescriptorContext) _
    As TypeConverter.StandardValuesCollection
    Dim f As New List(Of Foods)()
    f.Add(Foods.Eggs)
    f.Add(Foods.Meat)
    f.Add(Foods.Vegetables)
    Return New TypeConverter.StandardValuesCollection(f)
  End Function
End Class

In C#:

public class MyTypeConverter : TypeConverter
{
  public override TypeConverter.StandardValuesCollection GetStandardValues  (ITypeDescriptorContext context)
  {
     List<Foods> f = new List<Foods>();
     f.Add(Foods.Eggs);
     f.Add(Foods.Meat);
     f.Add(Foods.Vegetables);
     return new TypeConverter.StandardValuesCollection(f);
  }
}
Note
Note

You can also achieve similar results by returning a collection of arbitrary strings and then override the ConvertTo/ConvertFrom/CanConvert methods of the type converter and convert the strings to and from the enum values.

TypeConverter

The following topics provide additional information related to this topic.

Topic Purpose

This topics explains how to bind the control to a Collection.

This topics explains how to bind the control to a DataSet.

This topics explains how to bind the control to an XmlDataProvider.

This topics explains how to add items in the control’s dropdown by declaring them in XAML.