如何防止InvokeCommandAction将事件传播到父元素? [英] How to prevent InvokeCommandAction from propagating event to parent elements?

查看:88
本文介绍了如何防止InvokeCommandAction将事件传播到父元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到,当使用与EventTrigger关联的InvokeCommandAcction时,原始事件仍会路由到父元素,直到被处理为止.好吧,我想这是一种预期的行为.但是我的问题是如何将事件标记为已处理",以便事件不会在整个UI树中传播?

I realised that when using an InvokeCommandAcction associated to an EventTrigger, the original event was still routing up to the parent elements until it is handled. Well, I guess it is an expected behavior. But my question is how I can mark the event as Handled so it does not propagate up through the whole UI tree?

实际上,当您在命令中处理此事件时,所有内容都将在此命令中处理,因此不需要传播.在我发现的一个极端情况下,它会导致一些不必要的行为.例如,当用户双击一个元素(MouseDoubleClick事件)时,我会打开一个新窗口.问题在于新窗口将打开,然后主窗口又回到新窗口的前面,这是因为MouseDoubleClick事件刚刚到达UI树中的顶部元素.所需的行为是将新窗口保持在最前面,但是随着InvokeCommandAction让事件向上传播,主窗口将焦点移回了……

Actually, as you handle this event in a command, everything will be handled in this command, therefore it does not need to propagate. And in one corner case I found, it causes some unwanted behavior. For example, I open a new window when a user double click an element (MouseDoubleClick event). The problem is that the new windows opens and then the main window come back in front of the new one because the MouseDoubleClick event just reached the top element in the UI tree. The wanted behavior would be to keep the new window in front, but as the InvokeCommandAction lets the event propagate up, the main window takes back the focus...

我能做的是改为使用CallMethodAction资产,但是就像在MVVM场景中一样,我不需要代码中的UI事件参数.即使这会让我隐式地将事件标记为已处理并解决问题.

What I could do is to use the CallMethodAction asset instead but as I am in a MVVM scenario, I don't want UI event arguments in my code. Even if this would let me implicitely mark the event as handled and fix the issue.

<UserControl x:Class="..."
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDoubleClick">
            <i:InvokeCommandAction Command="{Binding Path=DisplayReportCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    ...
</UserControl>

推荐答案

您可以实现自己的EventTrigger,将事件标记为已处理.

You could implement your own EventTrigger that marks events as handled.

public class HandlingEventTrigger : System.Windows.Interactivity.EventTrigger
{
    protected override void OnEvent(System.EventArgs eventArgs)
    {
        var routedEventArgs = eventArgs as RoutedEventArgs;
        if (routedEventArgs != null)
            routedEventArgs.Handled = true;

        base.OnEvent(eventArgs);
    }
}

然后将<i:EventTrigger EventName="MouseDoubleClick">替换为<local:HandlingEventTrigger EventName="MouseDoubleClick">并添加

xmlns:local="clr-namespace:HandlingEventTrigger's namespace here"

您的用户控件的属性.

这篇关于如何防止InvokeCommandAction将事件传播到父元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆