RoutedCommands执行和PreviewExecuted事件 [英] RoutedCommands Executed and PreviewExecuted events

查看:98
本文介绍了RoutedCommands执行和PreviewExecuted事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我想在多个地方处理命令.例如,我有我的自定义UserControl,其中Button绑定到了某些命令.我在该控件中有一个命令绑定,但是在使用此控件的Window中也有一个命令绑定.

My problem is that I would like to handle a commands in multiple places. For instance I have my custom UserControl where a Button is bound to some command. I have a command binding in that control but I also have a command binding in a Window which uses this control.

我的目标是在控件内执行一些操作,同时又不中断窗口中命令的处理.

My aim is to perform some action inside the control while not interrupting handling of the command in the Window.

我尝试过Executed和PreviewExecuted事件,但是没有运气.然后,我在一个窗口(下面发布的代码)中模拟了这个问题.

I tried experimenting with Executed and PreviewExecuted events but with no luck. Then I simulated the problem in a single window (code posted below).

<Window x:Class="CommandingEvents.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:CommandingEvents="clr-namespace:CommandingEvents" 
    Title="Window1" Height="300" Width="300">
<Window.CommandBindings>
    <CommandBinding 
        Command="{x:Static CommandingEvents:Window1.Connect}"
        Executed="CommandBindingWindow_Executed"
        PreviewExecuted="CommandBindingWindow_PreviewExecuted"/>
</Window.CommandBindings>
<Grid>
    <Grid.CommandBindings>
        <CommandBinding 
        Command="{x:Static CommandingEvents:Window1.Connect}"
        Executed="CommandBindingGrid_Executed"
        PreviewExecuted="CommandBindingGrid_PreviewExecuted" />
    </Grid.CommandBindings>
    <Button Command="{x:Static CommandingEvents:Window1.Connect}" 
            CommandTarget="{Binding RelativeSource={RelativeSource Self}}"
            Content="Test" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>

namespace CommandingEvents
{
    public partial class Window1
    {
        public static readonly RoutedUICommand Connect = new
            RoutedUICommand("Connect", "Connect", typeof(Window1));

        public Window1()
        {
            InitializeComponent();
        }

        private void CommandBindingWindow_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Console.WriteLine("CommandBindingWindow_Executed");
            e.Handled = false;
        }

        private void CommandBindingGrid_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Console.WriteLine("CommandBindingGrid_Executed");
            e.Handled = false;
        }

        private void CommandBindingWindow_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            Console.WriteLine("CommandBindingWindow_PreviewExecuted");
            e.Handled = false;
        }

        private void CommandBindingGrid_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            Console.WriteLine("CommandBindingGrid_PreviewExecuted");
            e.Handled = false;
        }
    }
}

当我按下按钮时,仅输出"CommandBindingWindow_PreviewExecuted".这是为什么?我试图将e.Handled设置为false,但没有任何区别.谁能解释这种行为?

When I hit the button only "CommandBindingWindow_PreviewExecuted" is printed out. Why is that? I tried to set e.Handled to false but it doesn't make a difference. Can anyone explain this behaviour?

推荐答案

我不知道为什么会发生这种情况(以及它不是bug),但这是

I have no idea why that happens (and how it isn't a bug) but here is what was written in the WPF wiki:

有一个特殊之处 CommandBinding非常极端 有趣且重要的认识.

There is a particularity about CommandBinding which is extremely interesting and important to know.

CommandManager使用路由事件 通知不同的CommandBinding 执行命令的对象 调用(通过默认手势, 输入绑定,显式等).

The CommandManager uses routed events to notify the different CommandBinding objects that a command execution was invoked (through default gestures, input bindings, explicitly, etc.).

到目前为止,这还算不错 直截了当.但是,更重要的是 重要的是CommandBinding将 标记来自 CommandManager尽快处理 处理程序被执行(要么 预览已执行或已执行).

Up to now, this is fairly straightforward. However, what is more important is that CommandBinding will mark the routed event from the CommandManager as handled as soon as a handler gets executed (either PreviewExecuted or Executed).

最后,即使您的处理程序有一个 匹配委托的原型 称为ExecutedRoutedEventHandler, 来自CommandBinding的已执行事件 不是RoutedEvent,而是普通的CLR 事件.设置或离开 e.handled标志为false将会改变 什么都没有.

Finally, even if your handler has a prototype that matches a delegate called ExecutedRoutedEventHandler, the Executed event from the CommandBinding is not a RoutedEvent but a normal CLR event. Setting or leaving the e.Handled flag to false will change nothing.

因此,只要已执行或 调用PreviewExecuted处理程序, RoutedCommand将停止其 路由.

Therefore, as soon as an Executed or PreviewExecuted handler is invoked, the RoutedCommand will halt its routing.

这篇关于RoutedCommands执行和PreviewExecuted事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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