Skip to content

Latest commit

 

History

49 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ComboBox for Blazor - Bind to an enumeration

This example demonstrates how to create a wrapper class that obtains enumeration values and passes them to the ComboBox component's Data property.

Bind Combobox to Enum

Overview

Follow the steps below to bind the ComboBox component to an enumeration:

  1. Create a wrapper class with two properties that specify an enumeration value and a text string that the ComboBox displays.

    public class EducationDegree {
        // Specifies an enumeration value
        public EducationType Value { get; set; }
        // Specifies a text string
        public string DisplayName { get; set; }
    }
  2. Create a generic extension method that gets the DisplayAttribute.Name property value from the enumeration's member.

  3. Add the ComboBox component to your project and override the OnInitialized lifecycle method. This method creates a match between enumeration member integer and string values.

    @code {
        // ...
    
        protected override void OnInitialized() {
            //...
            EducationDegrees = Enum.GetValues(typeof(EducationType))
                .OfType<EducationType>()
                .Select(t => new EducationDegree()
                {
                    Value = t,
                    DisplayName = t.GetAttribute<DisplayAttribute>().Name
                }).ToList();
            base.OnInitialized();
        }
    }

Files to Review

Documentation

Does This Example Address Your Development Requirements/Objectives?

(you will be redirected to DevExpress.com to submit your response)

About

Create a wrapper class that obtains enumeration values and passes them to the ComboBox component's Data property.

Topics

Resources

Stars

0 stars

Watchers

33 watching

Forks

Releases

Packages

Used by

Contributors

Languages