为什么 e.Handled = true 不起作用?
我在后面的 XAML 代码中执行
<StackPanel MouseEnter="StackPanel_MouseEnter" Height="130" Background="Blue">
<Grid MouseEnter="Grid_MouseEnter" Height="60" Background="Red" >
<Button MouseEnter="Button_MouseEnter" Height="20"/>
</Grid>
</StackPanel>
此操作
private void StackPanel_MouseEnter(object sender, MouseEventArgs e)
{
}
private void Grid_MouseEnter(object sender, MouseEventArgs e)
{
e.Handled = true;
}
private void Button_MouseEnter(object sender, MouseEventArgs e)
{
e.Handled = true;
}
现在,即使我将鼠标移到 Button
上并设置 e.Handled = true
,Grid
的事件code> 和 StackPanel
分别被调用。为什么?我应该怎么做才能阻止路由事件冒泡?
I have following XAML
<StackPanel MouseEnter="StackPanel_MouseEnter" Height="130" Background="Blue">
<Grid MouseEnter="Grid_MouseEnter" Height="60" Background="Red" >
<Button MouseEnter="Button_MouseEnter" Height="20"/>
</Grid>
</StackPanel>
In code behind I am doing this
private void StackPanel_MouseEnter(object sender, MouseEventArgs e)
{
}
private void Grid_MouseEnter(object sender, MouseEventArgs e)
{
e.Handled = true;
}
private void Button_MouseEnter(object sender, MouseEventArgs e)
{
e.Handled = true;
}
Now even if I move mouse over Button
and set e.Handled = true
, the events of Grid
and StackPanel
are called respectively. Why? What should I do to stop routed event from bubbling up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MouseEnter 事件不是冒泡事件,它是直接事件(如经典的 CLR 事件)。来自文档:
所以你无法阻止它被解雇给父母。您可以使用 IsMouseDirectlyOver 属性来查看鼠标是否确实在但仅在给定元素上。
The MouseEnter event is not a bubbling event, it is a direct event (like classic CLR events). From the documentation:
So you can't prevent it from being fired on the parents. You can use the IsMouseDirectlyOver property to see if the mouse is in fact only over the given element though.