#854 – Clicked vs. Checked/Unchecked Events for CheckBox

If you want to add an event handler for a CheckBox that gets executed whenever a user clicks on the CheckBox, you can take one of two different approaches:

  • Add a handler for the Click event and then check the IsChecked property to discover the new state (checked / unchecked / indeterminate)
  • Handle one or more of the specific events for the individual states–Checked, Unchecked or Indeterminate

When a user clicks on the CheckBox, the specific event (e.g. Checked) is fired, followed by the Click event.

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            string checkState;

            if (!cb.IsChecked.HasValue)
                checkState = "Indeterminate";
            else
            {
                if (cb.IsChecked == true)
                    checkState = "Checked";
                else
                    checkState = "Unchecked";
            }

            Trace.WriteLine(string.Format("Click event: {0}", checkState));
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            Trace.WriteLine("Checked event");
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            Trace.WriteLine("Unchecked event");
        }

        private void CheckBox_Indeterminate(object sender, RoutedEventArgs e)
        {
            Trace.WriteLine("Indeterminate event");
        }

854-001

#661 – ButtonBase.Click Event vs. Mouse Click Events

Controls that derive from ButtonBase, like the standard Button, inherit a Click event that fires when user clicks on the button using the left mouse button.

Because ButtonBase inherits from UIElement, a Button will also have access to all of the mouse button events defined for UIElement.

Because the Button does something in response to button presses, it swallows the bubbling events (e.g. MouseLeftButtonDown and MouseDown).  You can still detect these lower level button press events by adding handlers for the tunneling events (e.g. PreviewMouseLeftButtonDown and PreviewMouseDown).  You can also request that your handlers are called even for already-handled events by specifying a handler in code and setting the handledEventsToo parameter to true.

For a left-click on a Button, you’ll normally see click-related events in the following order:

  • UIElement.PreviewMouseLeftButtonDown 
  • UIElement.PreviewMouseDown
  • UIElement.PreviewMouseLeftButtonUp
  • UIElement.PreviewMouseUp
  • ButtonBase.Click

#298 – Button Basics – Content and Click

The Button control in WPF represents a visual representation of a button that a user can click on.  It will typically initate some action.

Here’s an example, with two buttons.

You specify a button’s content–typically a text label–using the Content property.  Like other controls that descend from FrameworkElement, you can specify the height and width using the Height and Width properties.

		<Button Content="Save" Height="25" Width="80" />

You can execute some piece of code when the user clicks on the button by adding an event handler for the button’s Click event.

First, attach the handler in the XAML:

		<Button Content="Save" Height="25" Width="80" Click="Save_Click"/>

Then, define the corresponding method in the code-behind class:

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("You clicked on the Save button");
        }