views:

2174

answers:

4

I'm creating a UserControl consisting of a TextBox and a ListView. I want keyboard focus to remain with the TextBox as long as the control has keyboard focus (selection changes in the ListView shouldn't remove keyboard focus from the TextBox).

I've tried catching GotKeyboardFocus in the ListView and passing keyboard focus back to the TextBox using Keyboard.Focus(), but this seems to cancel any selection operation in the ListView. 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);
        }
    }
}
A: 

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

siz
I tried that. In that case, you can select from the ListView, but focus is removed from the keyboard.
Joseph Sturtevant
+1  A: 

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.

KP Adrian
I 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.
Joseph Sturtevant
+2  A: 

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();
}
Bubblewrap
A: 

Put Focusable=false on your ListView.