how to enable keyboard focus for selected item in listview?

70 views Asked by At

i have a winui/uwp app with a listview, when i use keyboard for navigate between items there is a white border around selected item, how i can enable this border for selected item by default? so i dont need to use keyboard. i dont want to use BorderThickness/BorderBrush in my ItemTemplate. i just want to enable keyboard focus by default enter image description here

1

There are 1 answers

0
Andrew KeepCoding On

Naming your ListView, getting the selected item container, then setting Focus should work.

public MainPage()
{
    this.InitializeComponent();
    this.Loaded += MainPage_Loaded;
}

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    int selectedIndex = this.ListViewControl.SelectedIndex;

    if (this.ListViewControl.ContainerFromIndex(selectedIndex) is UIElement selectedElement)
    {
        selectedElement.Focus(FocusState.Programmatic);
    }
}