Home
Map
CheckBox ExampleUse the WPF CheckBox control with the Checked and Unchecked event handlers.
WPF
This page was last reviewed on Jun 28, 2021.
CheckBox, WPF. A CheckBox allows an option to be set, or unset. This control by default has 2 states: Checked and Unchecked—an Indeterminate state is also possible.
Shows a checkboxShows a checkbox
Changes, IsChecked. We use event handlers (CheckBox_Checked and Unchecked) to detect changes. IsChecked tells us the current state.
RadioButton
XAML example. We create a new WPF project and add a CheckBox control. In the XAML section, add the "Checked" attribute and allow Visual Studio to create the CheckBox_Checked event handler.
Then Please do the same for CheckBox_Unchecked. Look at the C# code file for your project. It has been modified.
Shows a checkbox
<Window x:Class="WpfApplication6.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/> </Grid> </Window>
Code example. The CheckBox_Checked and CheckBox_Unchecked methods call a third method, Handle(), that deals with CheckBox changes. They pass the "sender" object cast to a CheckBox.
Detail We use the IsChecked property (a nullable bool) to determine the current state of the CheckBox in the Handle method.
Finally We assign to the Window Title property. This changes the title to indicate the state of the check box after it changes.
Shows a checkbox
using System.Windows; using System.Windows.Controls; namespace WpfApplication6 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void CheckBox_Checked(object sender, RoutedEventArgs e) { Handle(sender as CheckBox); } private void CheckBox_Unchecked(object sender, RoutedEventArgs e) { Handle(sender as CheckBox); } void Handle(CheckBox checkBox) { // Use IsChecked. bool flag = checkBox.IsChecked.Value; // Assign Window Title. this.Title = "IsChecked = " + flag.ToString(); } } }
IsThreeState. The CheckBox optionally supports 3 states. Please add the IsThreeState attribute to the XAML. This third state is called the "indeterminate" state.
Next We add a CheckBox_Indeterminate event handler in the same way as Checked and Unchecked.
Tip If you have trouble making up your mind, the "Indeterminate" option is a perfect choice.
<Window x:Class="WpfApplication6.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" Indeterminate="CheckBox_Indeterminate" IsThreeState="True"/> </Grid> </Window>
using System.Windows; namespace WpfApplication6 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void CheckBox_Checked(object sender, RoutedEventArgs e) { this.Title = "Checked"; } private void CheckBox_Unchecked(object sender, RoutedEventArgs e) { this.Title = "Unchecked"; } private void CheckBox_Indeterminate(object sender, RoutedEventArgs e) { this.Title = "Indeterminate"; } } }
Summary. Many CheckBox controls require only the Checked and Unchecked events. But the Indeterminate state is also available. CheckBox presents an option to the user.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 28, 2021 (rewrite).
Home
Changes
© 2007-2024 Sam Allen.