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

时间:2016-05-15 14:41:42

标签: c# wpf listbox separator

我有一个在启动时填充的列表框(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>

1 个答案:

答案 0 :(得分:0)

尝试以下代码。这是我完成的一个样本,它应该可以工作。

<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>