Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 815
Joan Venge Avatar asked Apr 07 '11 00:04

Joan Venge


3 Answers

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

like image 148
Femaref Avatar answered Nov 18 '22 20:11

Femaref


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" />
like image 40
Bala R Avatar answered Nov 18 '22 19:11

Bala R


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

like image 1
Jens Avatar answered Nov 18 '22 19:11

Jens