Completed and Expected RoutedCommands Events

My problem is that I would like to process commands in several places. For example, I have a custom UserControl where Button is bound to some command. I have a command binding in this control, but I also have a command binding in a window that uses this control.

My goal is to perform some actions inside the control without interrupting the processing of the command in the window.

I tried experimenting with Executed and PreviewExecuted events, but no luck. Then I simulated the problem in one window (the code 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; } } } 

When I click the button, only "CommandBindingWindow_PreviewExecuted" is printed. Why is this? I tried setting e.Handled to false, but that doesn't matter. Can anyone explain this behavior?

+6
wpf routed-commands
source share
1 answer

I have no idea why this is happening (and how it's not a mistake), but here is what was written in the WPF wiki :

There is a CommandBinding feature that is extremely interesting and important to know.

CommandManager uses routed events to notify other CommandBinding objects, the execution of commands is invoked (via default gestures, input bindings, explicitly, etc.).

So far, it is fairly simple. However, more importantly, CommandBinding will mark the routed event from the CommandManager as being processed as soon as the handler is 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 an ordinary CLR event. Setting or exiting the e.Handled flag to false will change nothing.

Therefore, as soon as the PreviewExecuted handler is executed or called, the RoutedCommand will stop routing.

+9
source share

All Articles