2016-05-15 72 views
0

我有一个在启动时被填充的列表框(C#WPF)。我希望在列表中的每个项目之间都有一个分隔符,并且我从另一个旧帖子中找到了此代码,并且它实际上正在工作,但是当我使用代码时,我会放弃突出显示和选定的颜色,而我无法弄清楚它在哪里出错了。带有分隔符的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> 

回答

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> 

+0

谢谢你的代码,但它没有任何不同,当隔膜是存在的,对悬停或选择无颜色 – user5860