What's the reason that MouseUp event doesn't occur, if I don't set the Background property in Grid?

huangapple go评论65阅读模式
英文:

What's the reason that MouseUp event doesn't occur, if I don't set the Background property in Grid?

问题

MouseUp事件通常会发生,如果我在Grid中设置了Background属性,但如果没有设置,事件就不会发生。你能解释一下为什么吗?

// 事件不会发生。
<Grid Name="MainGrid" MouseUp="MainGrid_MouseUp">
</Grid>
// 事件会发生。
<Grid Name="MainGrid" MouseUp="MainGrid_MouseUp" Background="AliceBlue">
</Grid>
private void MainGrid_MouseUp(object sender, MouseButtonEventArgs e)
{
  MessageBox.Show($"{e.GetPosition(this)}");
}
英文:

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&#39;t occurs.
&lt;Grid Name=&quot;MainGrid&quot; MouseUp=&quot;MainGrid_MouseUp&quot;&gt;
&lt;/Grid&gt;
// Event occurs.
&lt;Grid Name=&quot;MainGrid&quot; MouseUp=&quot;MainGrid_MouseUp&quot; Background=&quot;AliceBlue&quot;&gt;
&lt;/Grid&gt;
private void MainGrid_MouseUp(object sender, MouseButtonEventArgs e)
{
  MessageBox.Show($&quot;{e.GetPosition(this)}&quot;);
}

答案1

得分: 2

因为默认情况下,网格元素的背景被设置为 null (Panel.Background),因此不参与命中测试。通过将背景更改为透明或任何其他值,命中测试过程将生效,元素将有资格接收鼠标点击事件。

更多信息请参考这个SO问题

英文:

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

huangapple
  • 本文由 发表于 2023年2月16日 17:38:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定