WPF: Setting IsSelected for ListBox when TextBox has focus, without losing selection on LostFocus

c#, listbox, textbox, wpf, xaml

Solution

Best solution I've found to do this with no code behinde is this:

<Style TargetType="{x:Type ListBoxItem}">
    <Style.Triggers>
        <EventTrigger RoutedEvent="PreviewGotKeyboardFocus">
            <BeginStoryboard>
                <Storyboard>
                    <BooleanAnimationUsingKeyFrames
                        Storyboard.TargetProperty="(ListBoxItem.IsSelected)">

                        <DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Style.Triggers>
</Style>

Problem

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?

Original source