带有分隔符的WPF列表框,缺少突出显示的颜色



我有一个列表框(C#WPF),在启动时会填充。我希望在列表中的每一项之间都有一个分隔符,我从另一篇旧帖子中找到了这个代码,它实际上是有效的,但当我使用代码时,我会丢失突出显示和选择的颜色,我不知道哪里出了问题。

如何恢复高亮显示和选定的颜色?

这是我正在使用的代码。

<ListBox x:Name="radioBox" HorizontalAlignment="Left" Height="494" Margin="14,14,0,0" VerticalAlignment="Top" Width="287" Background="{x:Null}" FontFamily="Calibri" FontWeight="Thin" FontSize="25" Foreground="#FFEDEDF7" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" BorderThickness="1" Padding="30,30,0,0" >
            <ListBox.ItemBindingGroup>
                <BindingGroup/>
            </ListBox.ItemBindingGroup>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <StackPanel>
                                    <Separator x:Name="Separator" Background="White" Opacity="0.1" Height="20"/>
                                    <ContentPresenter/>
                                </StackPanel>
                                <ControlTemplate.Triggers>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">
                                        <Setter Property="Visibility" TargetName="Separator" Value="Collapsed"/>
                                    </DataTrigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>

请尝试以下代码。这是我做的一个样本,应该有效。

<Window x:Class="ListBoxStyle.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:ListBoxStyle"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="_Border"
                            Padding="2"
                            SnapsToDevicePixels="true">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="_Border" Property="Background" Value="Yellow"/>
                            <Setter Property="Foreground" Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ListBox ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
             Width="200" Height="250"
             ScrollViewer.VerticalScrollBarVisibility="Auto"
             ScrollViewer.HorizontalScrollBarVisibility="Auto">
        <ListBoxItem>Hello</ListBoxItem>
        <ListBoxItem>Hi</ListBoxItem>
    </ListBox>
</Grid>

最新更新