WPF supports keyboard focus

I am creating a UserControl consisting of a TextBox and a ListView . I want the keyboard focus to stay with the TextBox while the control has keyboard focus (selection changes in the ListView should not remove keyboard focus from the TextBox ).

I tried to catch GotKeyboardFocus in a ListView and pass keyboard focus to a TextBox using Keyboard.Focus(), , but this seems to cancel any selection operation in the ListView . The following code shows the problem. Does anyone know how to achieve this functionality?

 <Window x:Class="WpfApplication5.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <StackPanel> <TextBox x:Name="TextBox1" /> <ListView x:Name="ListBox1" Keyboard.GotKeyboardFocus="ListBox1_GotKeyboardFocus"> <ListViewItem Content="Able" /> <ListViewItem Content="Baker" /> <ListViewItem Content="Charlie" /> </ListView> </StackPanel> </Window> 

 using System.Windows; using System.Windows.Input; namespace WpfApplication5 { public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void ListBox1_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { Keyboard.Focus(TextBox1); } } } 
+4
source share
6 answers

It seems like you can change the focus in the MouseUp event. I think that if you do this too soon, as in the GotKeyboardFocus event, you will steal focus before the listview can handle the event and select the selected item.

 <StackPanel> <TextBox x:Name="TextBox1" /> <ListView x:Name="ListBox1" MouseUp="ListBox1_MouseUp"> <ListViewItem Content="Able" /> <ListViewItem Content="Baker" /> <ListViewItem Content="Charlie" /> </ListView> </StackPanel> private void ListBox1_MouseUp(object sender, MouseButtonEventArgs e) { TextBox1.Focus(); } 
+2
source

Instead, you think that you simply capture keystrokes and paste these keystrokes into your TextBox?

 <Window PreviewKeyDown="Window_PreviewKeyDown" > <Grid> <TextBox x:Name="TextBox1" /> <ListBox /> </Grid> </Window> 

Then in your window behind the code:

 private void Window_PreviewKeyDown(object sender, KeyEventArgs e) { TextBox1.Text += e.Key.ToString(); } 

You will need to do extra work for something like special characters (backspace, etc.) and obviously the Key handler for your "Enter" or "Post" operation, but this gives you the option of just free-type at the time how the window has focus and correctly handles keystrokes as needed.

+3
source

If you call your WPF window from WinForm, you should use this:

 System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(wpfWindow); wpfWindow.show(); 

from the MSDN documentation .

This is how I solved the keyboard problem.

ICEX

+1
source

This is a hack, but what if, instead of listening to the GotKeyboardFocus event, you are listening to the SelectionChanged event in a ListBox?

0
source

Put Focusable = false in your ListView.

0
source

Well, it drove me crazy. Even if you set the focus to UserControl every time the focus is lost, it still fails to get my keyboard shortcuts to work. All I had to do was set the Focusable property to true , and voilà to work!

0
source

All Articles