SIGN UP MEMBER LOGIN:    
ARTICLE

Bind a Struct to a ComboBox in C#

Posted by Mahesh Chand Articles | How do I February 08, 2010
This tip and code snippet shows how to bind a struct to a ComboBox in WPF and load its properties in the drop down.
Reader Level:

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()

{

    PixelFormatsList.Items.Clear();

    //Type t = typeof(System.Windows.Media.PixelFormats);

    //PropertyInfo[] props = t.GetProperties();

    foreach (PropertyInfo prop in typeof(System.Windows.Media.PixelFormats).GetProperties())

    {

        PixelFormatsList.Items.Add(prop.Name);

    }

    PixelFormatsList.SelectedIndex = 0;

}


You can use same approach to bind a struct to a Windows Forms or ASP.NET ComboBox or DropDownList controls.


Login to add your contents and source code to this article
share this article :
post comment
 

hello,there is a question i think several days,but haven't get the answer
just like this
<my:DataGridTemplateColumn Header="??"  Width="85">
                                <my:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding ClassName}" Name="tbCName" PreviewKeyDown="tbCName_PreviewKeyDown"/>
                                    </DataTemplate>
                                </my:DataGridTemplateColumn.CellTemplate>
                                <my:DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate>
                                        <ComboBox Name="comboCName"  IsEditable="True" IsTextSearchEnabled="False" ItemsSource="{Binding CNameList}" Text="{Binding ClassName}" PreviewKeyDown="comboCName_PreviewKeyUp" >

                                        </ComboBox>
                                    </DataTemplate>
                                </my:DataGridTemplateColumn.CellEditingTemplate>
                            </my:DataGridTemplateColumn>

i want to bindind DynamicResource to the comboBox in the datatemplate,if i edit in the UI,the dropdown of the combobox will changed,just like the google serch,when you enter the key words,there will droplist the info that revelant
 looking forward your help!
i will be very appreciate it!

Posted by caricaturecm Nov 26, 2010

hello,there is a question i think several days,but haven't get the answer
just like this
  <my:DataGridTemplateColumn Header="??"  Width="85">
                                <my:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding ClassName}" Name="tbCName" PreviewKeyDown="tbCName_PreviewKeyDown"/>
                                    </DataTemplate>
                                </my:DataGridTemplateColumn.CellTemplate>
                                <my:DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate>
                                        <ComboBox Name="comboCName"  IsEditable="True" IsTextSearchEnabled="False" ItemsSource="{Binding CNameList}" Text="{Binding ClassName}" PreviewKeyDown="comboCName_PreviewKeyUp" >

                                        </ComboBox>
                                    </DataTemplate>
                                </my:DataGridTemplateColumn.CellEditingTemplate>
                            </my:DataGridTemplateColumn>

i want to binging dynamicResource to the combox in the datatemplate,if i edit it in the UI,the dropdown of the combox will changed,just like the google search,when you enter the key words,there will droplist the info that revelant

look forward for your help!
i will be very appreciate it!

Posted by caricaturecm Nov 26, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor