我无法为列表框上选择的项目设置背景颜色。在这个例子中,我不想要交替的颜色。我把它们放进去做测试,它们起作用了。当Trigger IsSelected触发时,字体加粗并且前景变成红色。将高亮颜色刷子设置为SteelBlue无法实现所需的效果,因为当ListBox失去焦点时,它会消失。而红色和加粗则会保持,即使ListBox失去焦点,这正是我想要的。我希望选定的项目背景颜色能够生效并保持。目前,选定项目的背景为白色,并在ListBox失去焦点时保持。感谢您的帮助,我将测试任何提议的修复方案。
<ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Name="WFEnum" Visibility="Visible" BorderThickness="2" Margin="1" Padding="2,2,7,2"
ItemsSource="{Binding Path=SearchItem.SrchWorkFlows}" HorizontalAlignment="Left"
PresentationTraceSources.TraceLevel="High" AlternationCount="2" >
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="LightGreen"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="LightPink"></Setter>
</Trigger>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Background" Value="SteelBlue" />
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name, Mode=OneWay}" Background="Transparent" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>