其他控件下面的控件的单击事件

发布于 2024-09-07 15:30:22 字数 368 浏览 8 评论 0原文

我有两个控件 A 和 B。控件 B 放置在 A 上。单击 B 时,仅触发它的 clicked 事件,但是我也需要触发 A 的 click 事件。这可以通过使用路由事件来实现吗?

以下是控件的屏幕截图:

alt 文本 http://www.imagebanana.com/img /hu5znv4/buttons.png

当然,这种情况只是一个简化的示例,我正在寻找一种通用方法来在鼠标光标下方/一个位置的所有控件上引发事件。然而,这个问题的具体解决方案也会对我有帮助!非常感谢您的任何提示!

I have two controls A and B. Control B is placed over A. When B is clicked only it's clicked event is fired, however I need the click event of A to fire too. Can this somehow be realized using routed events?

Here's a screenshot of the controls:

alt text http://www.imagebanana.com/img/hu5znv4/buttons.png

Of course this case is just a reduced example, I'm looking for a general way to raise events on all controls below the mouse cursor / at one location. Hoewever, a concrete solution to this problem would help me too! Thank you very much for any hint!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同展鸳鸯锦 2024-09-14 15:30:22

如果内部控件的事件处理程序将 e.Handled 属性设置为 true,则可能会发生这种情况。这将防止路由事件进一步冒泡。但我刚刚检查过,在 WPF 4 中没有看到这种行为。

如果 e.Handled 阻止了您的事件,您可以做的是使用 UIElement.AddHandler 重载,采用布尔型handledEventsToo参数来挂钩您的点击事件。这必须在代码中完成,而不是标记。然后您应该在外部控件中收到 Click 事件,并且 e.Handled 属性已设置为 true。

当我单击内部按钮时,以下代码确实设置了两个按钮的背景。

<Grid>
    <Button VerticalAlignment="Center" HorizontalAlignment="Center" Click="Click1">
        <Button Width="100" Height="25" Margin="20" Click="Click2" />
    </Button>
</Grid>

代码隐藏

private void Click1( object sender, RoutedEventArgs e )
{
    ( (Button)sender ).Background = new SolidColorBrush( Colors.Blue );
}

private void Click2( object sender, RoutedEventArgs e )
{
    ( (Button)sender ).Background = new SolidColorBrush( Colors.Red );
    //e.Handled = true; // uncomment to stop bubbling
}

This can happen if the event handler of the inner control sets the e.Handled property to true. This would prevent the routed event from bubbling any further. But I just checked and I do not see that behavior in WPF 4.

If e.Handled is preventing your event, what you can do however is use the UIElement.AddHandler overload that takes a boolean handledEventsToo parameter to hook up your click event. This has to be done in code, not markup. Then you should receive the Click event in the outer control and the e.Handled property will already be set to true.

The following code does set the background of both buttons when I click the inner button.

<Grid>
    <Button VerticalAlignment="Center" HorizontalAlignment="Center" Click="Click1">
        <Button Width="100" Height="25" Margin="20" Click="Click2" />
    </Button>
</Grid>

Code Behind

private void Click1( object sender, RoutedEventArgs e )
{
    ( (Button)sender ).Background = new SolidColorBrush( Colors.Blue );
}

private void Click2( object sender, RoutedEventArgs e )
{
    ( (Button)sender ).Background = new SolidColorBrush( Colors.Red );
    //e.Handled = true; // uncomment to stop bubbling
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文