【问题标题】:What's the reason that MouseUp event doesn't occur, if I don't set the Background property in Grid?如果我不在 Grid 中设置 Background 属性,MouseUp 事件不会发生的原因是什么?
【发布时间】:2023-02-16 19:11:52
【问题描述】:

MouseUp 事件正常发生,如果我在Grid 中设置Background 属性,但如果不设置,则事件不会发生。你能解释一下这是为什么吗?

// 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)}");
}

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    这是因为网格元素的背景默认设置为null(Panel.Background),所以它不会发生在命中测试中。通过将背景更改为透明或任何其他值,命中测试过程将生效并且该元素将符合鼠标单击事件的条件。

    在这个SO Question有更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      • 2011-08-06
      • 1970-01-01
      • 2017-02-12
      • 2016-05-10
      相关资源
      最近更新 更多