nikotromus
nikotromus

Reputation: 1054

how to capture an event for an entire grid

I have a grid that takes up like a quarter of an interface. I want to capture an event whenever anyone clicks anywhere withing this grid (or a quarter of the interface). I created a stackpanel and I'vev set the PreviewMouseDown button, but it's not capturing the event when I click anywhere within the space for the grid. What am I doing wrong here?

         <Grid >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>

            </Grid.RowDefinitions>

            <StackPanel Grid.Row="0" Grid.RowSpan="3"
                        Name="sp_activationSpace"
                        PreviewMouseDown="testing"></StackPanel>

             ......

Upvotes: 1

Views: 59

Answers (1)

I don't know, how the rest of the window looks like, but what I usually do is create a Rectangle with usually white background (I don't think it can be transparent but I'm not sure) that is under the Grid I want to capture MouseButtonDown or PreviewMouseDown. It could look like this:

<Grid>
    <Rectangle PreviewMouseDown="MyGrid_PreviewMouseDown" Margin="0"/>
    <Grid>
        <!--your grid-->
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>

        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0" Grid.RowSpan="3"
                    Name="sp_activationSpace"
                    PreviewMouseDown="testing"></StackPanel>

         ......
    </Grid>
</Grid>

Again, this is a simple and well-functioning solution, however it might not be suitable if you need transparency. I suspect you have just a white (or any solid color) window background.

Upvotes: 1

Related Questions