(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

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

(1 votes)
Ivan Dragoev
>
Ivan Dragoev
Joined Oct 25, 2007
Articles:   7
Comments:   9
More Articles
4 comments   /   posted on Jun 11, 2008
Categories:   General , Controls

This article is compatible with the latest version of Silverlight.

While digging into templating and styling of Silverlight 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"
            HorizontalAlignment="Stretch">
    <TextBlock Text="Antonio Moreno"
                Height="22"
                FontSize="16"
                HorizontalAlignment="Left" />
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
 
        <TextBlock Text="AMoreno@hotmail.com"
                    Grid.Column="0"
                    HorizontalAlignment="Left" />
        <TextBlock Text="(5) 555-3932"
                    Grid.Column="1"
                    HorizontalAlignment="Left" />
    </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_1"
            Background="Transparent"
            HorizontalAlignment="Stretch">

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.


Subscribe

Comments

  • -_-

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


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

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


    posted by uuuuuuu on May 12, 2010 04:59
    StackPanel_MouseLeftButtonDown
  • -_-

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


    posted by Radu on Sep 10, 2010 11:23

    In a way this should be the behaviour. With a transparent background you have a "WireStackPanel".

    What I do is set a background color and Alpha to 0

  • lnikolov

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


    posted by lnikolov on Jan 03, 2011 16:50
    The article has been updated to the latest version of Silverlight and Visual Studio.

Add Comment

Login to comment:
  *      *       
Login with Facebook