Skip to main content
A newer version of this page is available. .
.NET Framework 4.5.2+

How to: Set Images and Captions for Enumeration Values

  • 3 minutes to read

This topic describes how to associate custom SVG or raster images and display captions with enumeration values. Refer to the Add and Override Images help topic for information on how to add and override UI images.

Enumeration as a Property Type

You can use the ImageNameAttribute to set the image that represents an enumeration value in a UI. Apply this attribute to the required enumeration values and specify the images to be used:

public enum SampleEnum {    
    [ImageName("BO_Person")]
    First,  
    [ImageName("BO_Position")]
    Second, 
    [ImageName("BO_Employee")]
    Third 
}

EnumImages

Note

  • Use either SVG or raster images for all items in one collection. When using SVG and raster images simultaneously, an enumeration property editor can fail to show certain images. In this case, make sure image files for all entries have the same format and check the application’s Log File for an additional diagnostic message the enumeration property editor recorded.
  • Blazor applications do not show images for enumeration values.

Use the XafDisplayNameAttribute to set the display caption that represents an enumeration value in a UI. Apply this attribute to the required enumeration values and specify the caption to be used:

public enum SampleEnum {    
    [XafDisplayName("John")]
    First,  
    [XafDisplayName("Sam")]
    Second, 
    [XafDisplayName("Bob")]
    Third 
}

EnumCaptions

Enumeration as Options Source for a SingleChoiceAction

Note

You can see the code demonstrated here in XAF‘s Main Demo.

Enumeration values can represent a Single Choice Action’s items. The following code demonstrates how to populate the ChoiceActionBase.Items collection with enumeration values, and set images for these items:

using DevExpress.ExpressApp.Utils;
//...
public partial class TaskActionsController : ViewController {
    public SetPriorityController() {
        InitializeComponent();
        SetPriorityAction.Items.Clear();   
        foreach(Priority current in Enum.GetValues(typeof(Priority))) {
            // Create an EnumDescriptor to get the localized enumeration value
            EnumDescriptor ed = new EnumDescriptor(typeof(Priority));
            ChoiceActionItem item = new ChoiceActionItem(ed.GetCaption(current), current);
            // Set the image name
            item.ImageName = ImageLoader.Instance.GetEnumValueImageName(current);
            SetPriorityAction.Items.Add(item);
        }
    }
}

The following image shows the SetPriorityAction in the Windows Forms application:

ImagesForEnum_7

See Also