
Working with CheckBoxes and RadioButtons
CheckBox is-a ToggleButton, which is-a ButtonBase. This may seem very odd given that the UI of a button
looks very different from that of a check box. However, a CheckBox type, like a Button, can be clicked,
responds to mouse and keyboard input, and follows the WPF content model. Given all of these similarities, it
turns out that the CheckBox type simply overrides various virtual members of ToggleButton to establish a
‘check box’ look and feel.
RadioButton is another type that is-a ToggleButton. Unlike the CheckBox type, however, it has the innate
ability to ensure all RadioButtons in the same container are mutually exclusive. When you wish to have a single
container with multiple RadioButton types that behave as distinct physical groupings, you can do so by setting
the GroupName property on the opening element of the RadioButton type.
<StackPanel>
<!-- The Music group. -->
<Label FontSize = "15" Content = "Select Your Music Media"/>
<RadioButton GroupName = "Music" >CD Player</RadioButton>
<RadioButton GroupName = "Music" >MP3 Player</RadioButton>
<RadioButton GroupName = "Music" >8-Track</RadioButton>
<!—The Color group. -->
<Label FontSize = "15" Content = "Select Your Color Choice"/>
<RadioButton GroupName = "Color">Red</RadioButton>
<RadioButton GroupName = "Color">Green</RadioButton>
<RadioButton GroupName = "Color">Blue</RadioButton>
</StackPanel>
When you design a collection of radio buttons or check boxes, it is common to surround them with a visual
container to denote that they behave as a group. The most common way to do so is by using a GroupBox
control. Notice the Header property allows you to embed any sort of content.
<StackPanel>
<GroupBox Header = "Select Your Music Media" BorderBrush = "Black">
<StackPanel>
<RadioButton GroupName = "Music" >CD Player</RadioButton>
<RadioButton GroupName = "Music" >MP3 Player</RadioButton>
<RadioButton GroupName = "Music" >8-Track</RadioButton>
</StackPanel>
</GroupBox>
<GroupBox BorderBrush = "Black">
<GroupBox.Header>
<Label Background = "Blue" Foreground = "White"
FontSize = "15" Content = "Select your color choice"/>
</GroupBox.Header>
<StackPanel>
<RadioButton>Red</RadioButton>
<RadioButton>Green</RadioButton>
<RadioButton>Blue</RadioButton>
</StackPanel>
</GroupBox>
</StackPanel>
In addition to the customary group box, WPF ships with a new UI element that can group a collection of UI
elements that can be hidden or shown via a toggle. The Expander type allows you to define the direction
elements will be displayed (up, down, left, or right) using the ExpandDirection property. The default
ExpandDirection is Down (used here).
<StackPanel>
<Expander Header = "Select Your Music Media" BorderBrush = "Black">
<StackPanel>
<RadioButton GroupName = "Music" >CD Player</RadioButton>
<RadioButton GroupName = "Music" >MP3 Player</RadioButton>
<RadioButton GroupName = "Music" >8-Track</RadioButton>
</StackPanel>
</Expander>
<Expander BorderBrush = "Black">
<Expander.Header>
<Label Background = "Blue" Foreground = "White"
FontSize = "15" Content = "Select your color choice"/>
</Expander.Header>
<StackPanel>
<RadioButton>Red</RadioButton>
<RadioButton>Green</RadioButton>
<RadioButton>Blue</RadioButton>
</StackPanel>
</Expander >
</StackPanel>
Working with ListBoxes and ComboBoxes
As you would expect, WPF provides types that contain a group of selectable items such as ListBox and
ComboBox, both of which derive from the ItemsControl abstract base class. Most importantly, this parent
class defines a property named Items, which returns a strongly typed ItemCollection object that holds onto
the sub items.
<!-- Simple list box. -->
<ListBox Name = "lstVideoGameConsoles">
<ListBoxItem>Microsoft XBox 360</ListBoxItem>
<ListBoxItem>Sony Playstation 3</ListBoxItem>
<ListBoxItem>Nintendo Wii</ListBoxItem>
<ListBoxItem>Sony PSP</ListBoxItem>
<ListBoxItem>Nintendo DS</ListBoxItem>
</ListBox>
<!-- Simple combo box. -->
<ComboBox Name = "comboVideoGameConsoles">
<ListBoxItem>Microsoft XBox 360</ListBoxItem>
<ListBoxItem>Sony Playstation 3</ListBoxItem>
<ListBoxItem>Nintendo Wii</ListBoxItem>
<ListBoxItem>Sony PSP</ListBoxItem>
<ListBoxItem>Nintendo DS</ListBoxItem>
</ComboBox>
If you need to dynamically fill a list, simply use the Items collection. This is identical to what you would do
for an ASP.NET or Windows Forms application.
// C# code (VB code would be similar).
private void FillListBox()
{
// Add items to the listbox.
lstVideoGameConsoles.Items.Add("Microsoft XBox 360");
lstVideoGameConsoles.Items.Add("Sony Playstation 3");
lstVideoGameConsoles.Items.Add("Nintendo Wii");
lstVideoGameConsoles.Items.Add("Sony PSP");
lstVideoGameConsoles.Items.Add("Nintendo DS");
}
To obtain the values in a list, use the SelectedIndex, SelectedItem, and SelectedValue properties. Assume the
following button Click event handler, which extracts data from the list of video games.
// C# code (VB code would be similar).
protected void btnGetGameSystem_Click(object sender, RoutedEventArgs args)
{
string data = string.Empty;
data += string.Format("SelectedIndex = {0}\n",
lstVideoGameConsoles.SelectedIndex);
data += string.Format("SelectedItem = {0}\n",
lstVideoGameConsoles.SelectedItem);
data += string.Format("SelectedValue = {0}\n",
lstVideoGameConsoles.SelectedValue);
MessageBox.Show(data, "Your Game Info");
}
As it turns out, the ItemCollection type has been constructed to operate on System.Object types, and,
therefore, it can contain anything whatsoever. Consider this example:
<StackPanel>
<!-- A ListBox with content. -->
<ListBox Name = "lstColors">
<StackPanel Orientation = "Horizontal">
<Ellipse Fill = "Yellow" Height = "50" Width = "50"/>
<Label FontSize = "20" HorizontalAlignment = "Center"
VerticalAlignment = "Center">Yellow</Label>
</StackPanel>
<StackPanel Orientation ="Horizontal">
<Ellipse Fill = "Blue" Height = "50" Width = "50"/>
<Label FontSize = "20" HorizontalAlignment = "Center"
VerticalAlignment = "Center">Blue</Label>
</StackPanel>
<StackPanel Orientation = "Horizontal">
<Ellipse Fill = "Green" Height = "50" Width = "50"/>
<Label FontSize = "20" HorizontalAlignment = "Center"
VerticalAlignment = "Center">Green</Label>
</StackPanel>
</ListBox>
</StackPanel>
CheckBoxes | RadioButtons | ListBoxes | ComboBoxes
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Training Resources
Tutorials
Services
Courseware