wpf 为 ListBox 中的选定项设置背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7059526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set background color for selected items in a ListBox
提问by paparazzo
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.
我无法为列表框上的所选项目设置背景颜色。我不想要这个例子中的交替颜色。我把它们作为测试,它们起作用了。Trigger IsSelected 正在触发,因为 fontweight 变为粗体,前景变为红色。将高亮颜色画笔设置为 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>
回答by Fredrik Hedblad
You specify the SelectedItem Background for a ListBox
with the SystemColors.HighlightBrushKey (focused) and SystemColors.ControlBrushKey (not focused)
您使用ListBox
SystemColors.HighlightBrushKey (focused) 和 SystemColors.ControlBrushKey (notfocused)为 a 指定 SelectedItem 背景
<Style.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Green"/>
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="LightGreen" />
</Style.Resources>
回答by H.B.
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">SteelBlue</SolidColorBrush>
</ListBox.Resources>
If you want this to apply out of focus as well you need to override an additional key:
如果您希望这也适用于焦点之外,您需要覆盖一个额外的键:
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}">SteelBlue</SolidColorBrush>