Recommended

  • Silverlight 4 Podcast Pack with Tim Heuer
  • Building Modular Silverlight Applications
  • Prism -  10 Things to Know
  • Securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques
  • Model– View – ViewModel in Silverlight
Skip Navigation LinksHome / Articles / View Article

Tip: Force firing MouseLeftButtonDown event on any place in a StackPanel

+ Add to SilverlightShow Favorites
1 comments   /   posted by Ivan Dragoev on Jun 11, 2008
(0 votes)
Categories: Announcements , Charts , Tools

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.

Share


Comments

Comments RSS RSS
  • RE: Tip: Force firing MouseLeftButtonDown event on any place in a StackPanel  

    posted by Rana on May 13, 2009 19:15
    Nice. Thanks!

Add Comment

 
 

   
  
  
   
Please add 6 and 7 and type the answer here: