Share via


How to: Create a ComboBox

In the following example, the ComboBox contains three items. The control is in its collapsed position, which is the default state, until the user clicks the Button on the right side of the control.

Example

<ComboBox Text="Is not open">
    <ComboBoxItem MouseMove="OnHover" Name="cbi1">Item1</ComboBoxItem>
    <ComboBoxItem MouseMove="OnHover" Name="cbi2">Item2</ComboBoxItem>
    <ComboBoxItem MouseMove="OnHover" Name="cbi3">Item3</ComboBoxItem>
</ComboBox>
cbox = new ComboBox();
cbox.Background = Brushes.LightBlue;
cboxitem = new ComboBoxItem();
cboxitem.Content = "Created with C#";
cbox.Items.Add(cboxitem);
cboxitem2 = new ComboBoxItem();
cboxitem2.Content = "Item 2";
cbox.Items.Add(cboxitem2);
cboxitem3 = new ComboBoxItem();
cboxitem3.Content = "Item 3";
cbox.Items.Add(cboxitem3);

cv2.Children.Add(cbox);
Dim cbox As System.Windows.Controls.ComboBox
Dim cboxitem As System.Windows.Controls.ComboBoxItem
Dim cboxitem2 As System.Windows.Controls.ComboBoxItem
Dim cboxitem3 As System.Windows.Controls.ComboBoxItem

cbox = New ComboBox()
cbox.Background = Brushes.LightBlue
cboxitem = New ComboBoxItem()
cboxitem.Content = "Created with Visual Basic."
cbox.Items.Add(cboxitem)
cboxitem2 = New ComboBoxItem()
cboxitem2.Content = "Item 2"
cbox.Items.Add(cboxitem2)
cboxitem3 = New ComboBoxItem()
cboxitem3.Content = "Item 3"
cbox.Items.Add(cboxitem3)

cv2.Children.Add(cbox)

For the complete sample, see ComboBox Sample.

See Also

Reference

ComboBox
ComboBoxItem

Concepts

ComboBox Overview