title: How to rewrite WPF routed commands dispatching mechanism
body: <p>Can I extend WPF commands routing in a way so it would first check whether command can be invoked in the focused field and if not in some other (never changing)? Is there any hooks for that? Maybe you don't know whether that would work but saw something similar somewhere on the net and can spare the link?</p>
  
  <p><strong>Abstract example</strong></p>
  
  <p>For example if I would be writing a text editor with a side panel and panel would have the focus. If I would press Ctrl+G some command will be invoked because panel has command binding and focus (that normal WPF behavior). Also if I press Ctrl+H but this time panel have no command binding for invoked command. In this case I would want routing engine to switch to text editor and bubble same command there to.</p>
  
  <p><strong>Real example</strong></p>
  
  <p>I have a menu item which says Paste but focus is on the side panel. If I press menu command binded to panel will be executed. Suppose there is no appropriate command binded to panel. In this case I would like to paste into text editor.</p>
  
  <p><strong>Code</strong></p>
  
  <p>This code more or less represents this scenario. I would like to press Ctrl+H and execute CommandBinding_Executed_1</p>
  
  <p><em>Window1.xaml</em></p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:WpfApplication1"&gt;
      &lt;StackPanel&gt;        
          &lt;TextBox x:Name="textBlock1"&gt;
              &lt;TextBox.CommandBindings&gt;
                  &lt;CommandBinding Command="local:Window1.TestCommand" Executed="CommandBinding_Executed_1" /&gt;
                  &lt;CommandBinding Command="local:Window1.ForwardedTestCommand" Executed="CommandBinding_Executed_1" /&gt;
              &lt;/TextBox.CommandBindings&gt;
          &lt;/TextBox&gt;
          &lt;TextBox x:Name="textBlock2"&gt;
              &lt;TextBox.CommandBindings&gt;
                  &lt;CommandBinding Command="local:Window1.TestCommand" Executed="CommandBinding_Executed_2" /&gt;
              &lt;/TextBox.CommandBindings&gt;
              &lt;TextBox.InputBindings&gt;
                  &lt;KeyBinding Command="local:Window1.TestCommand" Gesture="Ctrl+G" /&gt;
                  &lt;KeyBinding Command="local:Window1.ForwardedTestCommand" Gesture="Ctrl+H" /&gt;
              &lt;/TextBox.InputBindings&gt;
          &lt;/TextBox&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><em>Window1.xaml.cs</em></p>
  
  <pre><code>using System.Windows;
  using System.Windows.Input;
  
  namespace WpfApplication1
  {
      public partial class Window1 : Window
      {
          public static RoutedUICommand TestCommand = new RoutedUICommand("TestCommand", "TestCommand", typeof(Window1));
          public static RoutedUICommand ForwardedTestCommand = new RoutedUICommand("ForwardedTestCommand", "ForwardedTestCommand", typeof(Window1));
  
          public Window1()
          {
              InitializeComponent();
          }
  
          private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e)
          {
              MessageBox.Show("CommandBinding_Executed_1");
          }
  
          private void CommandBinding_Executed_2(object sender, ExecutedRoutedEventArgs e)
          {
              MessageBox.Show("CommandBinding_Executed_2");
          }
      }
  }
  </code></pre>