c# wpf 禁用事件冒泡
我遇到以下问题:
当我将两个标签相互插入时:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我面前没有文档,但我认为如果将 MouseButtonEventArgs 对象标记为“已处理”,它会阻止事件沿着链向上移动。
应该很简单,
如果我对此有误,请有人纠正我。
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
Please somebody correct me if I am wrong about this.