Skip to content

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

License

DevExpress-Examples/blazor-dxcombobox-bind-to-enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •