WPF: object sender

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

It is possible to detect checkbox state changes using only one event handler.

When the checkbox is clicked, the event e together with the checkbox object is sent as parameter into the event handler.

As the event takes an object known as sender, we need to cast the sender to checkbox.

This is done by using the C# as syntax:

CheckBox cb = sender as CheckBox;

In the XAML file, the Checked and Unchecked statuses of the checkbox is handle by only one function HandleChange.

<Window x:Class="CheckBox1.MainWindow"
        Title="MainWindow" Height="350" Width="525">
        <CheckBox Name="cb" Checked="HandleChange" Unchecked="HandleChange" Margin="50" HorizontalAlignment="left" FontSize="24" >Check Box</CheckBox>

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

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

        private void HandleChange(object sender, RoutedEventArgs e)
            CheckBox cb = sender as CheckBox;

            bool flag=cb.IsChecked.Value;

            cb.Content = flag.ToString();

The output from running the above code is:



Series Navigation<< WPF: Checkbox isChecked
WPF: Submitting textbox content >>

Leave a comment

Leave a Reply