Joan Venge
Joan Venge

Reputation: 331062

Separate events for checked and unchecked state of WPF CheckBox: Why?

  1. Is there a single event like Changed that I can use to handle both events together?

  2. Why are they separated like this?
    Is it because having a single event for both would requires you to reference the control by name, which you would need to specify in the XAML, and this would increase the clutter?

Upvotes: 23

Views: 16756

Answers (3)

Jens
Jens

Reputation: 505

For example to start a storyboard when checked and stop it when unchecked.

Upvotes: 1

Bala R
Bala R

Reputation: 108957

The split gives more granularity for those who need it (can't hurt for those who don't) and if you want you can handle both events with one handler.

 <CheckBox Content="CheckBox" Name="checkBox1" Checked="checkBox1_changed" Unchecked="checkBox1_changed" />

Upvotes: 16

Femaref
Femaref

Reputation: 61437

  1. Not directly. However, you can use the same event handler for both, and query the IsChecked property of the sender parameter (after casting it to CheckBox or ToggleButton of course).
  2. the two events are required for wpf specific technology, like storyboard, EventTriggers and similar. EventTriggers can't distinguish between state, only by event, so two different events are needed.

On a general note: I wouldn't use the events at all - I would bind the IsChecked property to an appropiate property on your ViewModel, keeping your code-behind to a minimum (Ideally no custom code at all).

Upvotes: 29

Related Questions