Add this to your ListBox.Resources
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
EDIT
The previous method only makes ListBoxItem selected as long as it has keyboard focus. If you move focus from a ListBoxItem, it will be canceled again.
Here's another easy way to select a ListBox when the keyboard focus moves inside the element and it remains selected when the focus is removed from the ListBoxItem.
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
</Style>
And in the code below
protected void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e)
{
ListBoxItem item = (ListBoxItem)sender;
item.IsSelected = true;
}
source
share