设置列表框中所选项目的背景颜色
我无法设置列表框中所选项目的背景颜色。我不想要这个例子中的交替颜色。我把它们作为测试,它们起作用了。当字体粗细变为粗体且前景变为红色时,触发 IsSelected。将突出显示颜色画笔设置为 SteelBlue 无法达到所需的效果,因为当列表框失去焦点时它会消失。当列表框失去焦点时,红色和粗体确实有效,这就是我想要的。我希望背景颜色能够保留所选项目。现在,所选项目的背景为白色,并在列表框失去焦点时保持不变。感谢您的帮助,我将测试任何建议的修复。
<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>
I cannot set the background color for the selected item on a list box. I don't want the alternating colors in this example. I put them in as a test and they work. Trigger IsSelected is firing as the fontweight goes to bold and the foreground goes to red. Setting highlight color brush to SteelBlue does not achieve the desired effect as it goes away when the ListBox loses focus. Red and bold does hold when the ListBox loses focus and is what I want. I want the background color to take and hold for the selected item. Right now the background for the selected items is white and holds when the ListBox loses focus. Thanks for your help and I will test any proposed fix.
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 SystemColors.HighlightBrushKey(聚焦)和 SystemColors.ControlBrushKey(未聚焦)为
ListBox
指定 SelectedItem 背景You specify the SelectedItem Background for a
ListBox
with the SystemColors.HighlightBrushKey (focused) and SystemColors.ControlBrushKey (not focused)如果您希望它也适用于焦点之外,您需要覆盖一个附加键:
If you want this to apply out of focus as well you need to override an additional key: