Set a border around selecteditem in listbox WPF

2019-04-30 12:15发布

问题:

How do i set a style on the listbox to get a border around the selected item ?

回答1:

The easiest way is to add a Trigger for IsSelected in the ItemContainerStyle for the ListBox

<ListBox ...>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="BorderBrush" Value="Red"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="BorderThickness" Value="1"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
    <!--...-->
</ListBox>


回答2:

FocusVisualStyle may be what are you looking for.