locked
HOME/END key bindings are intercepted by the middle control RRS feed

  • Question

  • i have a WPF application, on the very top Frame level I defined a bunch of key bindings. one of them is

    <KeyBinding Gesture="HOME" Command="myCommand"/>

    the app has a tab control and each tab has an user control. Inside the user control, user pressed "HOME" key trying to execute 'myCommand' defined in the keyBinding. But the command is intercepted by the tab control and instead of executing 'myCommand', tab control put the focus to the first tab. 

    is there a way to bypass the tab control so that i can reach the real 'myCommand' I wanted?

    Wednesday, November 20, 2013 9:23 PM

Answers

  • Hi,

    In the UserControl, we should add KeyBinding in the UserControl.InputBindings

    <UserControl ... x:Name="uc" Loaded="uc_Loaded">
         <UserControl.InputBindings>
            <KeyBinding Gesture="Home"  Command="{x:Static local:UserControl1.TestCommand}"/>
         </UserControl.InputBindings>
    ...

    And to make shortkey works well, we should also make it be focused:

    Private Sub uc_Loaded(sender As Object, e As RoutedEventArgs)
     Me.Focusable = True
     Keyboard.Focus(Me)
    End Sub

    Code Behind:

    Public Shared _testCommand As ICommand = New MyCommand()
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Public Shared ReadOnly Property TestCommand() As ICommand
    
            Get
                Return _testCommand
            End Get
        End Property
    
        Public Class MyCommand
            Implements ICommand
    
            Public Function CanExecute(parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
                Return True
            End Function
    
            Public Custom Event CanExecuteChanged As EventHandler Implements System.Windows.Input.ICommand.CanExecuteChanged
    
                AddHandler(ByVal value As EventHandler)
                        AddHandler CommandManager.RequerySuggested, value
                End AddHandler
                RemoveHandler(ByVal value As EventHandler)
                        RemoveHandler CommandManager.RequerySuggested, value
                End RemoveHandler
    
                RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
                    'This is the RaiseEvent block
                    CommandManager.InvalidateRequerySuggested()
                End RaiseEvent
            End Event
    
            Public Sub Execute(parameter As Object) Implements System.Windows.Input.ICommand.Execute
                MessageBox.Show("Success")
            End Sub
        End Class

    Download Sample:
    http://sdrv.ms/17PfHvu

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 21, 2013 9:23 AM
  • The reason for this is that the TabControl handles some key bindings on its own. You could override the default behaviour by adding a keybinding to the InputBindings collection of the TabControl:
     public partial class MainWindow : Window
        {
            public static readonly RoutedUICommand myCommand = new RoutedUICommand();
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void OnCommand(object target, ExecutedRoutedEventArgs e)
            {
                MessageBox.Show("...");
            }
        }

    <Window.CommandBindings>
            <CommandBinding Command="{x:Static local:MainWindow.myCommand}" Executed="OnCommand"/>
        </Window.CommandBindings>
            
            <TabControl x:Name="tabC">
                <TabControl.InputBindings>
                    <KeyBinding Command="{x:Static local:MainWindow.myCommand}" Gesture="HOME" />
                </TabControl.InputBindings>
                <TabItem Header="1">
                    <TextBlock>1</TextBlock>
                </TabItem>
                <TabItem Header="2">
                    <TextBlock>2</TextBlock>
                </TabItem>
            </TabControl>

    Thursday, November 21, 2013 8:22 AM

All replies

  • The reason for this is that the TabControl handles some key bindings on its own. You could override the default behaviour by adding a keybinding to the InputBindings collection of the TabControl:
     public partial class MainWindow : Window
        {
            public static readonly RoutedUICommand myCommand = new RoutedUICommand();
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void OnCommand(object target, ExecutedRoutedEventArgs e)
            {
                MessageBox.Show("...");
            }
        }

    <Window.CommandBindings>
            <CommandBinding Command="{x:Static local:MainWindow.myCommand}" Executed="OnCommand"/>
        </Window.CommandBindings>
            
            <TabControl x:Name="tabC">
                <TabControl.InputBindings>
                    <KeyBinding Command="{x:Static local:MainWindow.myCommand}" Gesture="HOME" />
                </TabControl.InputBindings>
                <TabItem Header="1">
                    <TextBlock>1</TextBlock>
                </TabItem>
                <TabItem Header="2">
                    <TextBlock>2</TextBlock>
                </TabItem>
            </TabControl>

    Thursday, November 21, 2013 8:22 AM
  • Hi,

    In the UserControl, we should add KeyBinding in the UserControl.InputBindings

    <UserControl ... x:Name="uc" Loaded="uc_Loaded">
         <UserControl.InputBindings>
            <KeyBinding Gesture="Home"  Command="{x:Static local:UserControl1.TestCommand}"/>
         </UserControl.InputBindings>
    ...

    And to make shortkey works well, we should also make it be focused:

    Private Sub uc_Loaded(sender As Object, e As RoutedEventArgs)
     Me.Focusable = True
     Keyboard.Focus(Me)
    End Sub

    Code Behind:

    Public Shared _testCommand As ICommand = New MyCommand()
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Public Shared ReadOnly Property TestCommand() As ICommand
    
            Get
                Return _testCommand
            End Get
        End Property
    
        Public Class MyCommand
            Implements ICommand
    
            Public Function CanExecute(parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
                Return True
            End Function
    
            Public Custom Event CanExecuteChanged As EventHandler Implements System.Windows.Input.ICommand.CanExecuteChanged
    
                AddHandler(ByVal value As EventHandler)
                        AddHandler CommandManager.RequerySuggested, value
                End AddHandler
                RemoveHandler(ByVal value As EventHandler)
                        RemoveHandler CommandManager.RequerySuggested, value
                End RemoveHandler
    
                RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
                    'This is the RaiseEvent block
                    CommandManager.InvalidateRequerySuggested()
                End RaiseEvent
            End Event
    
            Public Sub Execute(parameter As Object) Implements System.Windows.Input.ICommand.Execute
                MessageBox.Show("Success")
            End Sub
        End Class

    Download Sample:
    http://sdrv.ms/17PfHvu

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 21, 2013 9:23 AM