【发布时间】:2011-10-26 22:26:30
【问题描述】:
我无法为列表框中的选定项目设置背景颜色。我不想要这个例子中的交替颜色。我把它们作为测试,它们工作。当字体粗体变为粗体且前景变为红色时,触发器 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>
【问题讨论】:
标签: wpf xaml listbox background selecteditem