WPF: Checkbox status

This entry is part 15 of 54 in the series Learn WPF

A checkbox can either be checked or unchecked.

In this example, we build 2 event handlers to handle the 2 status of the checkbox.

<Window x:Class="CheckBox2.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="grid1" Margin="100,100,100,100">
    <TextBlock Name="textBlock1" Text="Check Box Status" HorizontalAlignment="right" Margin="0,0,60,60"></TextBlock>

using System.Windows;
using System.Windows.Controls;

namespace CheckBox2
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()

            CheckBox cb = new CheckBox();
            cb.Content = "CheckBox";
            cb.FontSize = 24;
            cb.Name = "cb_name";
            cb.Checked += cb_Checked;
            cb.Unchecked += cb_Unchecked;

        private void cb_Checked(object sender, RoutedEventArgs e)
            textBlock1.Text = "Checked";

        private void cb_Unchecked(object sender, RoutedEventArgs e)
            textBlock1.Text = "Unchecked";

In line 31, we have cb.Checked += cb_Checked.  This is to subscribe to the checkbox checked status using the + syntax in C#.  This event is handled by line 36.

The same goes to line 32 which is to subscribe to the unchecked status of the checkbox.

Clicking the checkbox will toggle the Check Box Status message.



Series Navigation<< WPF: Creating a checkbox control using code
WPF: XAML checkbox >>
Posted in