MouseDown doesnt work in Grid (only on buttons which in grids)
Asked Answered
O

3

8

I have problem with MouseDown event. My app looks like that, I have grid in which im adding buttons in code behind

<Grid Grid.Column="1" Name="gridWithButtons" MouseDown="normalModeButtonsWP_MouseDown" >
        <WrapPanel Name="normalModeButtonsWP" MouseDown="normalModeButtonsWP_MouseDown" ></WrapPanel>
</Grid>

But when im pressing mouse button in grid/wrappanel ( i mean in empty space between buttons for example) it doesnt work. Works only when im pressing button which is in wrap/grid. Anyone know how to handle it?

Overflow answered 1/10, 2012 at 8:25 Comment(0)
K
18

Setting IsHitTestVisible alone will not make it work.

Elements are not clickable if Background is set to None. To make it clickable (applies to grid, stackpanel, etc) set the Background to #00000000. This is a feature by design to prevent users clicking on ghost buttons. However, assigning it a color will make it clickable.

Kellar answered 6/9, 2013 at 6:52 Comment(1)
Or set Background to TransparentCitystate
D
0

Try setting IsHitTestVisible = true on your grid

Drayman answered 1/10, 2012 at 9:7 Comment(0)
G
0

In my fixed case, for a 3x3 grid with only one button in the middle, I added the PreviewMouseDown events for empty rows and columns in xaml:

<DataGridCell Grid.Row="0" PreviewMouseDown="EmptyAreaPreviewMouseDown"/>
<DataGridCell Grid.Row="2" PreviewMouseDown="EmptyAreaPreviewMouseDown"/>
<DataGridCell Grid.Column="0" PreviewMouseDown="EmptyAreaPreviewMouseDown"/>
<DataGridCell Grid.Row="2" PreviewMouseDown="EmptyAreaPreviewMouseDown"/>

and in code-behind:

private void EmptyAreaPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    // do here empty area work
}

Hope it helps someone in the future.

Gosnell answered 28/9, 2023 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.