TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

WPF CheckBox Example: Checked, Unchecked

This WPF article shows the CheckBox control. A CheckBox raises the Checked and Unchecked events.

CheckBox. A CheckBox allows an option to be set, or unset.

In WPF, a CheckBox by default has two states: Checked and Unchecked. We use event handlers (CheckBox_Checked and Unchecked) to detect changes. IsChecked tells us the current state.

Based on:

.NET 4.5

Example. First, we create a new WPF project and add a CheckBox control to it. In the XAML section of Visual Studio, 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.

Example markup: XAML

<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>

Example code: C#

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();
	}
    }
}

We see that both the CheckBox_Checked and CheckBox_Unchecked methods have the same code statement. They call a third method, Handle(), that deals with CheckBox changes. They pass the "sender" object cast to a CheckBox.

As

IsChecked: We use the IsChecked property (a nullable bool) to determine the current state of the CheckBox in the Handle method.

PropertyNullable Bool

Finally: We assign to the Window Title property. This changes the title to indicate the state of the checkbox after it changes.

IsThreeState. The CheckBox optionally supports three states. Please add the IsThreeState attribute to the XAML. This third state is called the "indeterminate" state. 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.

Example markup 2: XAML

<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>

Example code 2: C#

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 this element supports the Indeterminate state, rendered as a shaded box. CheckBox presents an option to the user in a WPF program.

RadioButton


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf