2016-05-15 46 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

関連する問題