I have a lots of controls in a Window including checkboxes and comboboxes. I want to track CheckBox.IsChecked event, so I defined an event handler in Windows level as
<Grid CheckBox.Checked="OnModified" CheckBox.Unchecked="OnModified">
The problem is that the same event is also fired by ComboBox right after mouse clicking an item. I thought my event handler should only capture CheckBox's Checked event, not ToggleButton's. Did I miss anything?
EDIT: As I pointed out below, I thought it would work this way because I read Matthew MacDonald's book "Pro WPF in C# 2010". On page 164, he gave this example code first:
<StackPanel Button.Click="DoSomething" Margin="5">
<Button Name="cmd1">Command 1</Button>
<Button Name="cmd2">Command 2</Button>
<Button Name="cmd3">Command 3</Button>
...
</StackPanel>
Then, he specially noted:
Note The Click event is actually defined in the ButtonBase class and inherited by the Button class. If you attach an event handler to ButtonBase.Click, that event handler will be used when any ButtonBase-derived control is clicked (including the Button, RadioButton, and CheckBox classes). If you attach an event handler to Button.Click, it’s only used for Button objects.
Now, did I misunderstand him, or is his note wrong?
There is not actually a separate
CheckBox.Checked
event. If you look at this page:and find the
Checked
event you will see that it is inherited fromToggleButton
soToggleButton.Checked
andCheckBox.Checked
are two different names for the same event.Since you are subscribing to a "wildcard" event, in the event handler you can check the sender or source to see you it is one you are interested in.
Edit:
To address your follow-on question regarding the book quote, I think the quote is at least misleading. Here is a counter-example that shows a
CheckBox
reacting toButton
event even thoughCheckBox
is not derived fromButton
:Of course there is no
Button.Click
event, only aButtonBase.Click
event, but that is the crux of the quote. If the quote were literally true, either this syntax would not be permitted or the event wouldn't fire, neither of which is true.