在ListBox中设置选定项的背景颜色。

7
我无法为列表框上选择的项目设置背景颜色。在这个例子中,我不想要交替的颜色。我把它们放进去做测试,它们起作用了。当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>
2个回答

14

ListBox 中,您可以使用 SystemColors.HighlightBrushKey(具有焦点)和 SystemColors.ControlBrushKey(无焦点)来指定所选项的背景。

<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> 

13
值得一提的是,截至2015年1月,这对于使用标准主题的System.Windows.Controls.ListBox的选择高亮颜色没有影响。触发器可以影响前景色但无法影响背景色。您需要重新定义ListBoxItem模板。 - 15ee8f99-57ff-4f92-890c-b56153
2020年的工作(但有点凌乱)解决方案:https://dev59.com/DlUItIcB2Jgan1zna-5v#64361991 - J23

10
<ListBox.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">SteelBlue</SolidColorBrush>
</ListBox.Resources>

如果您希望这也适用于失焦状态,您需要覆盖一个额外的键:

<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}">SteelBlue</SolidColorBrush>

我不得不接受另一种语法更加简洁的方式,但我还是给了你一个+1,谢谢。 - paparazzo
2
@BalamBalam:“干净”这个词是有争议的,不过我的答案比你快了七分钟,不管怎样,还是谢谢你。 - H.B.
1
@H.B. 抱歉,我实际上没有注意到你在我之前已经回答了几乎相同的内容.. 无论如何这里给你 +1。 - Fredrik Hedblad
@Meleak:没关系,真的,这种事情经常发生,实际上我们之前在其他地方也遇到过类似的情况,如果你还记得的话。 - H.B.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接