c# wpf 禁用事件冒泡

发布于 2024-08-26 08:36:23 字数 661 浏览 4 评论 0原文

我遇到以下问题:

当我将两个标签相互插入时:

<Label x:Name="First" MouseUp="Label_MouseUp">
    <Label x:Name="Second" MouseUp="Label_MouseUp_1">This is a label into another label</Label>
</Label>

以及以下代码:

private void Label_MouseUp(object sender, MouseButtonEventArgs e)
{
    Console.WriteLine("Do NOT show me");
}

private void Label_MouseUp_1(object sender, MouseButtonEventArgs e)
{
    Console.WriteLine("Show me");
}

当我单击“第二个”时,我希望它仅触发“Label_MouseUp_1”。但在我的控制台中我得到:

显示我
不要显示我

有没有办法关闭冒泡事件?

(另外,“First”必须是可点击的,因此删除那里的事件并不能解决问题)

Thnx

I'm having the following problem:

When I got two labels into each other:

<Label x:Name="First" MouseUp="Label_MouseUp">
    <Label x:Name="Second" MouseUp="Label_MouseUp_1">This is a label into another label</Label>
</Label>

And the following code:

private void Label_MouseUp(object sender, MouseButtonEventArgs e)
{
    Console.WriteLine("Do NOT show me");
}

private void Label_MouseUp_1(object sender, MouseButtonEventArgs e)
{
    Console.WriteLine("Show me");
}

When i click "Second", I want it to trigger only "Label_MouseUp_1". But in my console i get:

Show me
Do NOT show me

Is there a way to turn off the bubbling events?

(also, "First" has to be clickable, so removing the event there doesn't solve the problem)

Thnx

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

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

发布评论

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

评论(1

悲喜皆因你 2024-09-02 08:36:23

我面前没有文档,但我认为如果将 MouseButtonEventArgs 对象标记为“已处理”,它会阻止事件沿着链向上移动。

应该很简单,

e.Handled = true;

如果我对此有误,请有人纠正我。

I don't have the docs right in front of me, but I think if you mark the MouseButtonEventArgs object as Handled it stops the event from going up the chain.

Should be as simple as

e.Handled = true;

Please somebody correct me if I am wrong about this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文