While digging into templating and styling of Silverlight 2 Beta 2 controls, I came into a situation where the event for MouseLeftButtonDown was not triggered when I click on the control. I created a sample project and in the Page.xaml I added the following:
<StackPanel Orientation="Vertical"
MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
<TextBlock Text="Antonio Moreno" Height="22" FontSize="16" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="AMoreno@hotmail.com" Grid.Column="0"/>
<TextBlock Text="(5) 555-3932" Grid.Column="1"/>
</Grid>
</StackPanel>
As you can see from the xaml, I have a StackPanel and an event handler for MouseLeftButtonDown event. What I expect is when I click on the StackPanel or its children the event to be fired. Yes, but no. It works if I click on some of the children, but not when I click directly over the panel.
I found a way to fix that by adding a Background color to the StackPanel and setting it to Transparent:
<StackPanel Orientation="Vertical"
MouseLeftButtonDown="StackPanel_MouseLeftButtonDown"
Background="Transparent">
Having background color explicitly specified solves my problem and now I get the events on any place within the StackPanel.
For the demo I made two identical StackPanels with some other controls. The left one has no background color while the right one has. Click on the texts and then click on the empty space. The left part will not fire the event for the clicks over the empty space while the right one will fire the event on any place:
Download source.