Let me explain my requirements:
I have a grid containing several check boxes like this:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<CheckBox>1</CheckBox>
<CheckBox
Grid.Column="1">2</CheckBox>
</Grid>
I know clearly how to use mvvm light EventToCommand to bind to a command (which is defined in my VM or anywhere else). But, as we all know, I have more than 1 check box. So I have to do it like this:
<CheckBox
Content="1">
<i:Interaction.Triggers>
<i:EventTrigger
EventName="Checked">
<ml:EventToCommand
Command="{Binding SomeCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
<CheckBox
Grid.Column="1"
Content="2">
<i:Interaction.Triggers>
<i:EventTrigger
EventName="Checked">
<ml:EventToCommand
Command="{Binding SomeCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
If I have more check boxes, I have to C+V again and again so as to make each check box have the same EventToCommand behaviors. What's more annoying is that we also have another event named as Unchecked. Usually the callback for both Checked and Uncheck are totally the same. So I have to C+V again & again & again..........
And finally my XAML is long and complicated!
==============================================
I'm thinking of some workaround like this:
I looked into the source code of Checked as well as Unchecked event, and found that both of them were RoutedEvent with mode as Bubble, which means this routed event call bubble up to the grid which contains them. So I wrote my XAML code like this:
<Grid>
<i:Interaction.Triggers>
<i:EventTrigger
EventName="ToggleButton.Checked">
<ml:EventToCommand
Command="{Binding TestCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
......several check boxes......
</Grid>
But the above codes won't work. Neither Checked nor Unchecked is ATTACHED event.
Then I deleted "ToggleButton.", like this:
<i:EventTrigger
EventName="Checked">
<ml:EventToCommand
Command="{Binding TestCommand}" />
</i:EventTrigger>
Still not working! :(
Events should BUBBLE up to the grid, don't they?
So, anyone have some good ideas about this? Thanks!