禁用事件冒泡C#WPF [英] disable event-bubbling c# wpf
问题描述
我有以下问题:
当我有两个标签相互转化:
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>
和下面的代码:
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 。但是,在我的控制台我得到:
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
推荐答案
我没有文档权我的面前,但是我觉得如果你标记为已处理就会停止上升链事件的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.
应该是那样简单
e.Handled = true;
请别人纠正我,如果我错了这一点。
Please somebody correct me if I am wrong about this.
这篇关于禁用事件冒泡C#WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!