WPF: Checkbox isChecked

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

How do we know if a checkbox is checked, you may ask.

The WPF checkbox comes with a IsChecked boolean where we can use it to determine if a checkbox is checked or unchecked.

In the example below, we set the checkbox using IsChecked=’True”.

MainWindow.xaml
<Window x:Class="CheckBox1.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">

    <StackPanel>
        <Grid Name="grid1">
            <CheckBox Name="cb" IsChecked="True" Margin="50" HorizontalAlignment="left" FontSize="24" >Check Box</CheckBox>
        </Grid>
        <TextBlock Name="tB" Text="Check Box Status" HorizontalAlignment="right" Margin="50"></TextBlock>
    </StackPanel>
</Window>

MainWindow.xaml
using System.Windows;
using System.Windows.Controls;

namespace CheckBox1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            if (cb.IsChecked==true)
            {
                tB.Text = "Checkbox is checked";
            }
        }       
    }
}

The output from running the above code is:

isChecked

 

Series Navigation<< WPF: XAML checkbox
WPF: object sender >>

Leave a comment

Leave a Reply