Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF TextBox Intercepting RoutedUICommands
    primarykey
    data
    text
    <p>I am trying to get Undo/Redo keyboard shortcuts working in my WPF application (I have my own custom functionality implemented using the <a href="https://stackoverflow.com/questions/701131/data-structure-used-to-implement-undo-and-redo-option/701135#701135">Command Pattern</a>). It seems, however, that the <code>TextBox</code> control is intercepting my "Undo" RoutedUICommand.</p> <p>What is the simplest way to disable this so that I can catch Ctrl+Z at the root of my UI tree? I would like to avoid putting a ton of code/XAML into each <code>TextBox</code> in my application if possible.</p> <p>The following briefly demonstrates the problem:</p> <pre><code>&lt;Window x:Class="InputBindingSample.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:loc="clr-namespace:InputBindingSample" Title="Window1" Height="300" Width="300"&gt; &lt;Window.CommandBindings&gt; &lt;CommandBinding Command="loc:Window1.MyUndo" Executed="MyUndo_Executed" /&gt; &lt;/Window.CommandBindings&gt; &lt;DockPanel LastChildFill="True"&gt; &lt;StackPanel&gt; &lt;Button Content="Ctrl+Z Works If Focus Is Here" /&gt; &lt;TextBox Text="Ctrl+Z Doesn't Work If Focus Is Here" /&gt; &lt;/StackPanel&gt; &lt;/DockPanel&gt; &lt;/Window&gt; </code></pre> <hr> <pre><code>using System.Windows; using System.Windows.Input; namespace InputBindingSample { public partial class Window1 { public static readonly RoutedUICommand MyUndo = new RoutedUICommand("MyUndo", "MyUndo", typeof(Window1), new InputGestureCollection(new[] { new KeyGesture(Key.Z, ModifierKeys.Control) })); public Window1() { InitializeComponent(); } private void MyUndo_Executed(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("MyUndo!"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload