Sonhja
Sonhja

Reputation: 8448

Why mi ListBox is not recovering Focus?

I have a TextBox with a ListBox just below that acts as an autocomplete TextBox. For a better explanation, it would be great if you look at this video.

This is my code:

The xaml:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="200*" />
    </Grid.RowDefinitions>
    <TextBox 
        Name="TextAuto"
        Height="23" TextWrapping="NoWrap" Text="TextBox" VerticalAlignment="Top"
        PreviewKeyDown="TextAuto_OnPreviewKeyDown" />

    <ListBox Name="ListBoxSuggestion"
             Grid.Row="1" VerticalAlignment="Top"  Visibility="Collapsed" 
             PreviewKeyDown="ListBoxSuggestion_OnPreviewKeyDown"/>
</Grid>

The code behind:

public MainWindow()
{
    InitializeComponent();
    nameList = new List<string>
    {
        "A0-Word", "B0-Word", "C0-Word",
        "A1-Word", "B1-Word", "C1-Word",
        "A2-Word", "B2-Word", "C2-Word",
        "A3-Word", "B3-Word", "C3-Word"
    };
    TextAuto.TextChanged += TextAuto_TextChanged;
}

void TextAuto_TextChanged(object sender, TextChangedEventArgs e)
{
    string typedString = TextAuto.Text;
    List<string> autoList = new List<string>();
    autoList.Clear();

    autoList.AddRange(nameList.Where(item => !string.IsNullOrEmpty(TextAuto.Text)).Where(item => item.StartsWith(typedString)));

    if (autoList.Count > 0)
    {
        ListBoxSuggestion.ItemsSource = autoList;
        ListBoxSuggestion.Visibility = Visibility.Visible;
    }
    else 
    {
        ListBoxSuggestion.Visibility = Visibility.Collapsed;
        ListBoxSuggestion.ItemsSource = null;
    }
}

private void TextAuto_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    if (!e.IsDown || e.Key != Key.Down) return;

    FocusManager.SetFocusedElement(this, ListBoxSuggestion);
    ListBoxSuggestion.SelectedIndex = 0;

}

private void ListBoxSuggestion_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.IsDown && e.Key == Key.Enter)
    {
        if (ListBoxSuggestion.ItemsSource != null)
        {
            ListBoxSuggestion.Visibility = Visibility.Collapsed;
            TextAuto.TextChanged -= TextAuto_TextChanged;
            if (ListBoxSuggestion.SelectedIndex != -1)
            {
                TextAuto.Text = ListBoxSuggestion.SelectedItem.ToString();
            }
            TextAuto.TextChanged += TextAuto_TextChanged;
        }
    }

    if (!e.IsDown || e.Key != Key.Up) return;
    if (ListBoxSuggestion.SelectedIndex != 0) return;

    FocusManager.SetFocusedElement(this, TextAuto);
    ListBoxSuggestion.SelectedIndex = -1;
}

When in the TextBox, if down pressed you access the ListBox. In the ListBox, when going up, if the SelectedIndex==0, I give the focus back to the TextBox. But second time I want to give the focus back to the ListBox (second time I press down key inside the TextBox), the ListBox looks gray, and I can't access it... :(

But, as appreciated in the video, it seems as the ListBox never recovers the focus!! But when looking to the FocusManager.FocusedElement, it says the ListBox has the focus. Same happens with the Keyboard.Focus.

What can be happening?

Upvotes: 1

Views: 58

Answers (1)

Chubosaurus Software
Chubosaurus Software

Reputation: 8161

Add a IsTabStop="False" to your TextBox

<TextBox 
    Name="TextAuto"
    Height="23" TextWrapping="NoWrap" Text="TextBox" VerticalAlignment="Top"
    PreviewKeyDown="TextAuto_OnPreviewKeyDown" IsTabStop="False" />

It's basically getting TabFocused for some odd reason. Because Down Key is use to select objects on the Page as well.

Upvotes: 1

Related Questions