英文:
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'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)}");
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论