I implement a search for the characters entered from the database: when the user enters a letter, the words starting with the same letter are extracted from the database. The search works, the list of words appears, but the transfer of focus by pressing the down arrow from the input field to the selection options does not work. Illustration and sample code below.
XAML
<StackPanel Margin="0,5,0,0"> <TextBox x:Name="textBoxSearch" KeyUp="textBoxSearch_KeyUp" Width="250" Height="23" /> <Border x:Name="borderHint" BorderThickness="1" BorderBrush="Black" Width="250" Height="95" Margin="0,2,0,0" Visibility="Collapsed" > <ScrollViewer VerticalScrollBarVisibility="Auto"> <StackPanel x:Name="stackHint"></StackPanel> </ScrollViewer> </Border> </StackPanel> The beginning of the method where I try to shift the focus
/// <summary> /// Ввод текста в поле для поиска слова /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void textBoxSearch_KeyUp(object sender, KeyEventArgs e) { //если нажата стрелка вниз if (e.Key == Key.Down && borderHint.Visibility == Visibility.Visible) { //TODO: сделать перенос фокуса на первое слово в списке if (stackHint.Children.Count > 0) { scrollViewer.Focus(); //stackHint.Focus(); //stackHint.Children[0].Focus(); //var b = stackHint.Children[0].Focusable; } return; } //ссылка на вводимый текст string query = (sender as TextBox).Text; //очищаем бордер от предыдущих элементов this.stackHint.Children.Clear(); //управление видимостью бордера if (query.Length == 0) { //если ничего не введено, прячем бордер this.borderHint.Visibility = System.Windows.Visibility.Collapsed; return; } else { //если что-то введено, то показываем бордер this.borderHint.Visibility = System.Windows.Visibility.Visible; } //запрашиваем подходящие слова List<string> hints = await ((ListWordsViewModel)this.DataContext).GetHints(query); //показываем в зависимости от результата if (hints.Any()) { AddHints(hints); } else { this.stackHint.Children.Add(new TextBlock() { Text = "Ничего не найдено." }); } } private void AddHints(List<string> hints) { TextBlock block = null; foreach (var hint in hints) { //добавляем содержимое подсказки block = new TextBlock(); block.Text = hint; //стиль block.Cursor = Cursors.Hand; //События мыши block.MouseLeftButtonUp += (sender, e) => { //вводим выбранную подсказку this.textBoxSearch.Text = (sender as TextBlock).Text; //прячем остальные подсказки this.borderHint.Visibility = Visibility.Collapsed; //передаем нужный текст во ViewModel ((ListWordsViewModel)this.DataContext).SearchText = this.textBoxSearch.Text; }; block.MouseEnter += (sender, e) => { TextBlock b = sender as TextBlock; b.Background = Brushes.LightBlue; }; block.MouseLeave += (sender, e) => { TextBlock b = sender as TextBlock; b.Background = Brushes.Transparent; }; //отображение подсказки this.stackHint.Children.Add(block); } } Next in this method comes the extraction of the right words from the database and filling this.stackHint TextBlock with the right words. So I want to transfer the focus to the first TextBlock so that the arrow can be selected from the list. I went through the steps, the condition fulfills, but for some reason the focus in the interface does not change. What do you advise?
PS
if (stackHint.Children.Count > 0) { scrollViewer.Focus(); var b = stackHint.Children[0].Focusable; } The focus on the scrollViewer shifting, but it’s still impossible to go further with the arrow. But b == false

stackHint.Children[0].Focusable == false, then show how you add elements tostackHint. - VladD