我的问题是我想在多个地方处理命令。例如,我有自定义用户控件,其中按钮绑定到某个命令。我在该控件中有一个命令绑定,但在使用该控件的窗口中也有一个命令绑定。

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

我尝试尝试执行和预览执行事件,但没有成功。然后我在一个窗口中模拟了该问题(代码如下)。

<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 但没有什么区别。谁能解释这种行为?

有帮助吗?

解决方案

我不知道为什么会发生这种情况(以及它如何不是一个错误),但这是在 WPF 维基:

关于命令的特殊性非常有趣,很重要。

CommandManager使用路由事件来通知不同的命令对象,以指示执行命令执行(通过默认手势,输入绑定,明确的命令等)。

到目前为止,这相当简单。但是,更重要的是,命令贴键将在执行处理程序后立即从CommandManager中标记路由事件(PreviewExexeccectectectectectectectectectectectectectectectectexectectectectectectectectectectectectectectectectectectectectectectectectectectectectectectectectect ecectectect ecectectectect consect consecter complater bection commanager。

最后,即使您的处理程序具有与称为executedRoutedeventHandler的代表匹配的原型,但从命令键入的事件并不是RoutedEvent,而是正常的CLR事件。设置或将E.Handled Flag设置为false将不会改变。

因此,一旦调用了执行或预览的处理程序,路由命令就会停止其​​路由。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top