gpt4 book ai didi

wpf - RoutedCommands 已执行和预览执行事件

转载 作者:行者123 更新时间:2023-12-02 13:04:47 30 4
gpt4 key购买 nike

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

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

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

<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 wiki 中写的内容:

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

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

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).

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.

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

关于wpf - RoutedCommands 已执行和预览执行事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4670703/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com