I've a ListBox
with ListBoxItems
with a template so they contain TextBoxes
When the TextBox
gets focused I want the ListBoxItem
to be selected. One solution I've found looks like this:
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True"></Setter>
</Trigger>
</Style.Triggers>
</Style>
This works great, but when the TextBox
loses focus so does the selection.
Is there a way to prevent this from happening?
From the list of suggested solutions nothing helped me to resolve the same issue. This is the custom solution I made:
1). Create Behavior (class that holds attached properties) that is going to enforce the focus:
2). Use this behavior in your XAML:
Best solution I've found to do this with no code behinde is this:
You can also keep focus on the text box, but only have one ListBoxItem selected at any given time, with code behind.
In the ListBox XAML:
Then, in the
CheckFocus()
method in code-behind: