WPF Maintain Keyboard Focus
Solution 1
It looks like it's possible to change focus in the MouseUp event. I think if you do it too early, like in the GotKeyboardFocus event, you'll steal focus before the listview can handle the event and select the chosen 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();
}
Solution 2
Instead, have you considered just capturing keystrokes and putting those keystrokes into your TextBox?
<Window PreviewKeyDown="Window_PreviewKeyDown" >
<Grid>
<TextBox x:Name="TextBox1" />
<ListBox />
</Grid>
</Window>
Then in your window's code-behind:
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
TextBox1.Text += e.Key.ToString();
}
You'll have to do extra work for anything like special characters (backspace, etc), and obviously a Key handler for your "Enter" or "Post" operation, but it gives you the ability to just free-form type while the Window has focus and to properly handle the keystrokes as necessary.
Solution 3
If you are calling your WPF window from a WinForm you must use this:
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(wpfWindow);
wpfWindow.show();
from the MSDN documentation.
Thats how I solved my keyboard problem.
IceX
Related videos on Youtube
Joseph Sturtevant
Updated on April 16, 2022Comments
-
Joseph Sturtevant about 2 years
I'm creating a
UserControl
consisting of aTextBox
and aListView
. I want keyboard focus to remain with theTextBox
as long as the control has keyboard focus (selection changes in theListView
shouldn't remove keyboard focus from theTextBox
).I've tried catching
GotKeyboardFocus
in theListView
and passing keyboard focus back to theTextBox
usingKeyboard.Focus(),
but this seems to cancel any selection operation in theListView
. The below 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); } } }
-
Joseph Sturtevant about 15 yearsI think that would work. I would like, however, to have the cursor stay in the TextBox. The functionality I'm shooting for is similar to IntelliSense in Visual Studio.