KeyBinding in UserControl doesn't work when TextBox has the focus
Asked Answered
S

6

26

The following situation. I've got a UserControl with five keybindings. When the TextBox has the focus the keybindings of the UserControl stop firing..

Is there a way to fix this 'problem'?

<UserControl.InputBindings>
    <KeyBinding Key="PageDown" Modifiers="Control" Command="{Binding NextCommand}"></KeyBinding>
    <KeyBinding Key="PageUp" Modifiers="Control" Command="{Binding PreviousCommand}"></KeyBinding>
    <KeyBinding Key="End" Modifiers="Control"  Command="{Binding LastCommand}"></KeyBinding>
    <KeyBinding Key="Home" Modifiers="Control" Command="{Binding FirstCommand}"></KeyBinding>
    <KeyBinding Key="F" Modifiers="Control" Command="{Binding SetFocusCommand}"></KeyBinding>
</UserControl.InputBindings>
<TextBox Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.InputBindings>
        <KeyBinding Gesture="Enter" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl }}, Path=DataContext.FilterCommand}"></KeyBinding>
    </TextBox.InputBindings>
</TextBox>

It seems function keys (F1 etc) and ALT+[key] do work. I presume the CTRL and SHIFT modifiers are somehow 'blocking' the event from bubbling up to the UserControl.

Shuffleboard answered 17/10, 2012 at 19:17 Comment(2)
textbox is inside the usercontrol?Bantam
@DJ exactly. The UserControl is the container and (in this example) a TextBox is inside it. It also works for ComboBox, DataGrid etc.Shuffleboard
T
55

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 CTRL+V for paste, CTRL+Home for going to the beginning of the text, etc. Other key combinations such as CTRL+F3 on the other hand aren't handled by the TextBox, and so they will bubble up.

If you'd just wanted to disable the TextBox's input binding, that would be simple - you could use the ApplicationCommands.NotACommand command, which would disable the default behavior. For example, in the following case, pasting with CTRL+V will be disabled:

<TextBox>
    <TextBox.InputBindings>
        <KeyBinding Key="V" Modifiers="Control" Command="ApplicationCommands.NotACommand" />
    </TextBox.InputBindings>
</TextBox>

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

I wrote a basic behavior that achieves this functionality to get you started:

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<KeyBinding>()
            .FirstOrDefault(kb => kb.Key == e.Key && kb.Modifiers == e.KeyboardDevice.Modifiers);

        if (foundBinding != null)
        {
            e.Handled = true;
            if (foundBinding.Command.CanExecute(foundBinding.CommandParameter))
            {
                foundBinding.Command.Execute(foundBinding.CommandParameter);
            }
        }
    }
}

Usage:

<UserControl local:InputBindingsBehavior.TakesInputBindingPrecedence="True">
    <UserControl.InputBindings>
        <KeyBinding Key="Home" Modifiers="Control" Command="{Binding MyCommand}" />
    </UserControl.InputBindings>
    <TextBox ... />
</UserControl>

Hope this helps.

Tadzhik answered 17/10, 2012 at 21:2 Comment(5)
@AdiLester I have tried your solution. I just replaced your lambda expression with kb => kb.Key == Key.Enter. I also replaced Key in KeyBinding from Home to Enter. And removed Modifiers. My TextBox resides inside DataGridColumnHeader. So, I applied the attached property to DataGrid. Now, when I press any key other than Enter, for e.g. Down Arrow Key, the command fires. I just want my Command to fire when I press Enter Key. Can you please provide some changes to the above mentioned code????Commit
@Adi Lester PreviewKeyDown += new KeyEventHandler where do you unregsiter from the event?Neurath
@Neurath In the sense of memory leaks, there's no real need to unregister from the event since no object is being held. You are "holding" a reference to a static method which belongs to no actual object instance. Even assuming this wasn't a static method, you still wouldn't have to unregister from the event, since the InputBindingsBehavior instance will be freed as soon as the UIElement is no longer referenced from anywhere else.Tadzhik
@AdiLester Your example helped me a lot. I was trying lots of ideas over some days to achieve this result. Thank you.Uncharitable
@AdiLester That's exactly what I was looking for! thank a lot!!Apocope
L
5

Adi Lester's solution works well. Here's a similar solution using Behavior. The C# code:

public class AcceptKeyBinding : Behavior<UIElement>
{


    private TextBox _textBox;



    /// <summary>
    ///  Subscribes to the PreviewKeyDown event of the <see cref="TextBox"/>.
    /// </summary>
    protected override void OnAttached()
    {
        base.OnAttached();

        _textBox = AssociatedObject as TextBox;

        if (_textBox == null)
        {
            return;
        }

        _textBox.PreviewKeyDown += TextBoxOnPreviewKeyDown;
    }

    private void TextBoxOnPreviewKeyDown(object sender, KeyEventArgs keyEventArgs)
    {
        var uielement = (UIElement)sender;

        var foundBinding = uielement.InputBindings
            .OfType<KeyBinding>()
            .FirstOrDefault(kb => kb.Key == keyEventArgs.Key && kb.Modifiers ==           keyEventArgs.KeyboardDevice.Modifiers);

        if (foundBinding != null)
        {
            keyEventArgs.Handled = true;
            if (foundBinding.Command.CanExecute(foundBinding.CommandParameter))
            {
                foundBinding.Command.Execute(foundBinding.CommandParameter);
            }
        }
    }

    /// <summary>
    ///     Unsubscribes to the PreviewKeyDown event of the <see cref="TextBox"/>.
    /// </summary>
    protected override void OnDetaching()
    {
        if (_textBox == null)
        {
            return;
        }

        _textBox.PreviewKeyDown -= TextBoxOnPreviewKeyDown;

        base.OnDetaching();
    }

}

And the XAML:

<TextBox>
  <TextBox.InputBindings>
      <KeyBinding Key="Enter" Modifiers="Shift" Command="{Binding CommandManager[ExecuteCommand]}"
          CommandParameter="{Binding ExecuteText}" />
  </TextBox.InputBindings>
      <i:Interaction.Behaviors>
         <behaviours:AcceptKeyBinding />
      </i:Interaction.Behaviors>
</TextBox>
Lovelady answered 2/5, 2014 at 2:14 Comment(0)
M
5

In addition to Adi Lester his (very helpful) answer I would like to suggest some improvements/extensions that helped me with my implementation.

Gesture.Matches

The foundBinding can also be done by calling Gesture.Matches. Change the foundBinding Linq query to the following:

KeyBinding foundBinding = ((UIElement)this).InputBindings
            .OfType<KeyBinding>()
            .FirstOrDefault(inputBinding => inputBinding.Gesture.Matches(sender, eventArgs));

MouseBinding

Furthermore you can also define MouseBindings.

<MouseBinding Command="{Binding DataContext.AddInputValueCommand, ElementName=root}" CommandParameter="{Binding}" Gesture="Shift+MiddleClick" />

You then also need to subscribe to PreviewMouseEvents e.g. PreviewMouseUp and PreviewMouseDoubleClick. The implementation is then almost the same as for KeyBindings.

private void OnTextBoxPreviewMouseUp(object sender, MouseButtonEventArgs eventArgs)
{
    MouseBinding foundBinding = ((UIElement)this).InputBindings
        .OfType<MouseBinding>()
        .FirstOrDefault(inputBinding => inputBinding.Gesture.Matches(sender, eventArgs));

    if (foundBinding != null)
    {
        eventArgs.Handled = true;
        if (foundBinding.Command.CanExecute(foundBinding.CommandParameter))
        {
            foundBinding.Command.Execute(foundBinding.CommandParameter);
        }
    }
}
Marinmarina answered 7/12, 2016 at 15:48 Comment(0)
N
2

This Thread is old but many have this problem. My research has shown, that Adi Lester's Solution ist the only one which isn't a "dirty" Workaround. For anayone who needs, the VisualBasic.NET Implemantation:

Public Class InputBindingsBehavior
    Public Shared ReadOnly TakesInputBindingPrecedenceProperty As DependencyProperty = DependencyProperty.RegisterAttached("TakesInputBindingPrecedence", GetType(Boolean), GetType(InputBindingsBehavior), New UIPropertyMetadata(False, AddressOf OnTakesInputBindingPrecedenceChanged))

    Public Shared Function GetTakesInputBindingPrecedence(obj As UIElement) As Boolean
        Return obj.GetValue(TakesInputBindingPrecedenceProperty)
    End Function

    Public Shared Sub SetTakesInputBindingPrecedence(obj As UIElement, value As Boolean)
        obj.SetValue(TakesInputBindingPrecedenceProperty, value)
    End Sub

    Public Shared Sub OnTakesInputBindingPrecedenceChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
        AddHandler DirectCast(d, UIElement).PreviewKeyDown, AddressOf InputBindingsBehavior_PreviewKeyDown
    End Sub

    Public Shared Sub InputBindingsBehavior_PreviewKeyDown(sender As Object, e As KeyEventArgs)
        Dim uielement = DirectCast(sender, UIElement)

        Dim foundBinding = uielement.InputBindings.OfType(Of KeyBinding).FirstOrDefault(Function(kb As KeyBinding) kb.Key = e.Key And kb.Modifiers = e.KeyboardDevice.Modifiers)

        If foundBinding IsNot Nothing Then
            e.Handled = True
            If foundBinding.Command.CanExecute(foundBinding.CommandParameter) Then
                foundBinding.Command.Execute(foundBinding.CommandParameter)
            End If
        End If
    End Sub

End Class

The rest as mentioned.

Noaccount answered 8/4, 2020 at 10:36 Comment(0)
Z
0
<UserControl.Style>
    <Style TargetType="UserControl">
        <Style.Triggers>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="FocusManager.FocusedElement" Value="   {Binding ElementName=keyPressPlaceHoler}" />
                </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Style>

keyPressPlaceHoler is the name of container of your target uielement

remember to set the Focusable="True" in usercontrol

Zorina answered 12/6, 2016 at 18:10 Comment(0)
P
0

Code behind is the only way when it comes to textbox and I agree with answer of @adi-lester

 private void Control_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.SystemKey == Key.PageDown && (e.KeyboardDevice.Modifiers == ModifierKeys.Alt || .KeyboardDevice.Modifiers == ModifierKeys.Alt) )
        {
            //do something
        }
    }

.

anyTextBox.PreviewKeyUp += TabControl_PreviewKeyDown;
Pownall answered 18/6, 2022 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.