0

MouseUp event is occurred normally, if I set the Background property in Grid, but, if not, the event doesn't occur. Could you please explain me why it is?

// Event doesn't occurs.
<Grid Name="MainGrid" MouseUp="MainGrid_MouseUp">
</Grid>
// Event occurs.
<Grid Name="MainGrid" MouseUp="MainGrid_MouseUp" Background="AliceBlue">
</Grid>
private void MainGrid_MouseUp(object sender, MouseButtonEventArgs e)
{
  MessageBox.Show($"{e.GetPosition(this)}");
}
isakgo_
  • 750
  • 4
  • 15

1 Answers1

2

That is because by default the background of grid element is set to null (Panel.Background), so it does not take part in hit testing. By changing the background to transparent or any other value, the hit testing process will take effect and the element will be eligible for mouse click events.

There is more info at this SO Question

Palle Due
  • 5,929
  • 4
  • 17
  • 32
mehdi.loa
  • 579
  • 1
  • 5
  • 23