【问题标题】:WPF: Click on Usercontrols Background does not fireWPF:单击用户控件背景不会触发
【发布时间】:2015-01-21 10:11:38
【问题描述】:

我有一个 WPF 用户控件,它-simplified- 看起来像这样:

<UserControl Name="NiceUserControl" MouseLeftButtonUp="NiceUserControl_MouseLeftButtonUp">
    <Grid MouseDown="Grid_MouseDown">
        <Border>
           <A lot Of Different Stuff like Buttons and Other usercontrols>
        </Border>
    </Grid>
</UserControl>

像这样,当单击来自 UserControl 的子元素之一时,来自 Usercontrol 的事件“MouseLeftButtonUp”会触发,但当我单击控件的(空)背景时不会触发。 "Professional" Demo: 点击绿色元素会触发,红色背景不会:(

我也尝试添加这个事件处理程序,它的最后一个参数应该在事件已经处理时忽略:

        NiceUserControl.AddHandler(MouseDownEvent, new MouseButtonEventHandler(NiceUserControl_MouseDown), true);

但这仅有助于例如我的用户控件上的文本框,- 背景仍然不会触发。 甚至包含网格中的 PreviewMouse-Events 或 MouseEvents 也不行。

注意:例如,我希望控件内的按钮具有自己的单击事件,但我希望它们也触发用户控件单击事件。

【问题讨论】:

  • 您需要将UserControlGridBorder 上的Background 设置为默认空值以外的值,例如Transparent。否则它是不可见的命中测试。
  • @dkozl 其实就是这么简单。我没有考虑到这一点。谢谢!

标签: c# wpf events routing mouseevent


【解决方案1】:

这可能是因为您没有设置背景属性。

【讨论】:

  • 伙计,太棒了!谢谢!就这么简单-__- 至少我现在知道路由事件是如何工作的:D
  • @topograph 你能把它标记为已回答吗?谢谢!
  • 当然! Stackoverflow 说,我要等几分钟才能标记它;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 2016-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多