Providing options to select from a ComboBox
A ComboBox
control is an items control and works like ListBox
, but only one item from the list is selectable. A ListBox
control by default lists multiple items on screen, but ComboBox
control displays the scrollable list only on a user click. Thus, it takes up a lot less space.
This recipe will talk about ComboBox
control and how to use it.
Getting ready
Begin with creating a new WPF application project, called CH02.ComboBoxDemo
, using your Visual Studio IDE.
How to do it...
Follow these simple steps to add a ComboBox
control in your application UI:
- Replace the default
Grid
with aStackPanel
to host UI controls horizontally stacked. - Add the following XAML code, inside the
StackPanel
, to have a simpleComboBox
control with some items in it:
<ComboBox Width="150" Height="26" Margin="10"> <ComboBoxItem Content="Item 1" /> <ComboBoxItem Content="Item 2" IsSelected="True" /> <ComboBoxItem Content="Item 3" /> ...