Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason some input bindings work and some don't is that the TextBox control catches and handles some key bindings. For example, it handles <kbd>CTRL</kbd>+<kbd>V</kbd> for paste, <kbd>CTRL</kbd>+<kbd>Home</kbd> for going to the beginning of the text, etc. Other key combinations such as <kbd>CTRL</kbd>+<kbd>F3</kbd> on the other hand aren't handled by the TextBox, and so they will bubble up.</p> <p>If you'd just wanted to disable the TextBox's input binding, that would be simple - you could use the <code>ApplicationCommands.NotACommand</code> command, which would disable the default behavior. For example, in the following case, pasting with <kbd>CTRL</kbd>+<kbd>V</kbd> will be disabled:</p> <pre><code>&lt;TextBox&gt; &lt;TextBox.InputBindings&gt; &lt;KeyBinding Key="V" Modifiers="Control" Command="ApplicationCommands.NotACommand" /&gt; &lt;/TextBox.InputBindings&gt; &lt;/TextBox&gt; </code></pre> <p>However, making it bubble up to the user control is a bit trickier. My suggestion is to create an attached behavior that will be applied to the UserControl, register to its <code>PreviewKeyDown</code> event, and execute its input bindings as necessary before they reach the TextBox. This will give precedence to the UserControl when input bindings are executed.</p> <p>I wrote a basic behavior that achieves this functionality to get you started:</p> <pre class="lang-cs prettyprint-override"><code>public class InputBindingsBehavior { public static readonly DependencyProperty TakesInputBindingPrecedenceProperty = DependencyProperty.RegisterAttached("TakesInputBindingPrecedence", typeof(bool), typeof(InputBindingsBehavior), new UIPropertyMetadata(false, OnTakesInputBindingPrecedenceChanged)); public static bool GetTakesInputBindingPrecedence(UIElement obj) { return (bool)obj.GetValue(TakesInputBindingPrecedenceProperty); } public static void SetTakesInputBindingPrecedence(UIElement obj, bool value) { obj.SetValue(TakesInputBindingPrecedenceProperty, value); } private static void OnTakesInputBindingPrecedenceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((UIElement)d).PreviewKeyDown += new KeyEventHandler(InputBindingsBehavior_PreviewKeyDown); } private static void InputBindingsBehavior_PreviewKeyDown(object sender, KeyEventArgs e) { var uielement = (UIElement)sender; var foundBinding = uielement.InputBindings .OfType&lt;KeyBinding&gt;() .FirstOrDefault(kb =&gt; kb.Key == e.Key &amp;&amp; kb.Modifiers == e.KeyboardDevice.Modifiers); if (foundBinding != null) { e.Handled = true; if (foundBinding.Command.CanExecute(foundBinding.CommandParameter)) { foundBinding.Command.Execute(foundBinding.CommandParameter); } } } } </code></pre> <p>Usage:</p> <pre><code>&lt;UserControl local:InputBindingsBehavior.TakesInputBindingPrecedence="True"&gt; &lt;UserControl.InputBindings&gt; &lt;KeyBinding Key="Home" Modifiers="Control" Command="{Binding MyCommand}" /&gt; &lt;/UserControl.InputBindings&gt; &lt;TextBox ... /&gt; &lt;/UserControl&gt; </code></pre> <p>Hope this helps.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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