Bind a Struct to a ComboBox in C#
This tip and code snippet shows how to bind a struct to a ComboBox in WPF and load its properties in the drop down.
Often times when you need to load a structure (struct) members to a ComboBox or DropDownList so user can select a value and based on the selection, the resulted value will be applied.
In one of my WPF applications, I needed to load all PixelFormats structure members (properties) in a ComboBox and based on the selection of the PixelFormats, I will change the output of the image.
We can use the Type class to read a type members including methods, properties and other type members. A struct simply has some properties. As you can see from the following code snippet, I create a Type objet from the PixelFormats by using typeof keyword. Once a Type is created, I can use GetProperties method to get all properties of a struct.
The Type.GetProperties method returns an array of PropertyInfo. The Name property of PropertyInfo object gives us the name of the property.
Once we have an array of PropertyInto objects, we can simply loop through the properties and add them to a ComboBox one by one.
The following code snippet loads all members of PixelFormats struct to a ComboBox in WPF.
private void LoadPixelFormatsToComboBox()
//Type t =
props = t.GetProperties();
foreach (PropertyInfo prop in typeof(System.Windows.Media.PixelFormats).GetProperties())
PixelFormatsList.SelectedIndex = 0;
You can use same approach to bind a struct to a Windows Forms or ASP.NET ComboBox or DropDownList controls.