CheckBox

The CheckBox control represents a control that a user can select (check) and clear (uncheck). The CheckBox can have three states: checked, unchecked, and indeterminate. Use a CheckBox to give the user an option, such as true/false or yes/no, or to choose from a list of options. The CheckBox is a ContentControl.

The following illustration shows some check box controls in various states.

Silverlight CheckBox controls

Silverlight CheckBox controls

View a Running Sample

To view a running sample of the CheckBox control, click the following link.

Run this sample.

Creating a CheckBox control

Typically, you create the CheckBox and associate an event-handler with its Checked, Unchecked or Indeterminate event in Xaml. The following code demonstrates how to create a check box in Xaml, set its name attribute and Content property and associate its Checked event with a method to handle the event.

<CheckBox  x:Name="CheckBox1" Width="160" Content="Unchecked Checkbox" 
   Checked="CheckBox1_Checked" />

The following code shows the implementation of the CheckBox1_Checked method.

private void CheckBox1_Checked(object sender, RoutedEventArgs e)
{
    CheckBox cb = sender as CheckBox;
    if (cb.IsChecked == true)
       cb.Content = "Checked CheckBox";
    else
       cb.Content = "Unchecked CheckBox";
}

Additional Code Examples

You can accomplish the following tasks with the example code at the specified location.

Task

Example location

Creating a three-state ToggleButton control.

ToggleButton

Using styles with the CheckBox control.

CheckBox Styles and Templates

The topics in this section describe the additional concepts and techniques that you can use to build CheckBox control features into your applications.

Topic

Description

How to: Handle the Checked event

Shows how to handle Checked event for a CheckBox.

Reference

Topic

Description

CheckBox

Provides class library documentation for the CheckBox class.

ToggleButton

Provides a description of the base class for the CheckBox control

RadioButton

Provides class library documentation for another control used for user selection.