LOST
LOST

Reputation: 3250

Make selection follow focus in UWP ListView

I am trying to make ListView with single selection to make selection instantly follow keyboard focus.

I am looking at

https://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/listview-and-gridview#item-selection-and-interaction

However, the only similar documented mode is when focus is moved between elements with keyboard, but to select focused item user needs to hit one more key.

Is there an easy way to make keyboard move both focus and selection.

Upvotes: 1

Views: 955

Answers (2)

LOST
LOST

Reputation: 3250

@AVK's answer is valid for common case. However, instead I had to GotFocus="OnCommandGotFocus", probably because my list was grouped.

void OnCommandGotFocus(object sender, RoutedEventArgs e) {
  if (e.OriginalSource is ListViewItem command) {
    command.IsSelected = true;
  }
}

Upvotes: 1

AVK
AVK

Reputation: 3923

ListView contains a property called SingleSelectionFollowsFocus which when set to true will select the focused item automatically when used with keyboard.

Upvotes: 1

Related Questions