First item selection in autocompletebox silverlight

I have an autocompletebox silverlight that is filled with elements. Does anyone know how to select the first item in the list so that if the user presses the enter button, the item is selected and the user does not need to use a mouse?

In other Windows controls, you can use selectedindex = 0;

+4
source share
4 answers

For those of your interest, you need to get a link to the ListBox child control for AutoCompleteBox and use SelectedIndex to do this.

+2
source

In the XAML Set

IsTextCompletionEnabled = "True"

+2
source

I think you are looking for SelectedItem. If you do this in code, you just need something like autoCompleteControl.SelectedItem = listUsedToPopulate [0];

0
source

To talk in detail about the good answers that have already been given.

Firstly, jesse answer - set IsTextCompletionEnabled="True" , simple - fills the text field after each key press with the first element in the list. When you press the enter button, the popup closes. The reason I did not use this approach is because it immediately updates the SelectedItem , without waiting for the user to press the enter key.

Sico's answer is what I used. GetTemplateChild method requires subclassing the AutoCompleteBox control. Here is the code:

 public class ExtendedAutoCompleteBox : AutoCompleteBox { protected override void OnKeyDown(KeyEventArgs e) { if (e.Key == Key.Enter) { UpdateSelection(); } } private void UpdateSelection() { // get the source of the ListBox control inside the template var enumerator = ((Selector)GetTemplateChild("Selector")).ItemsSource.GetEnumerator(); // update Selecteditem with the first item in the list enumerator.Reset(); if (enumerator.MoveNext()) { var item = enumerator.Current; SelectedItem = item; // close the popup, highlight the text IsDropDownOpen = false; (TextBox)GetTemplateChild("Text").SelectAll(); } } } 
0
source

Source: https://habr.com/ru/post/1384596/


All Articles