WPF ListView 非活动选择颜色和元素字体颜色

4

我可以设置ListView的非活动选择颜色

我使用了以下问题描述的解决方案

WPF ListView Inactive Selection Color

我需要更改所选非活动元素的字体颜色,有没有简单的方法来实现这一点?

谢谢

2个回答

7
很抱歉,你不能使用SystemColors.ControlTextBrushKey,因为它只适用于项目未被选中时,或者当项目被选中但处于非活动状态时(你的问题似乎只关注后者)。然而,你可以这样做:
<ListBox ...>
    <ListBox.Resources>
        <!-- this customizes the background color when the item is selected but inactive -->
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}">Red</SolidColorBrush>
    </ListBox.Resources>
    <ListBox.ItemContainerStyle>
        <Style>
            <Style.Triggers>
                            <!-- this customizes the foreground color when the item is selected but inactive -->
                <Trigger Property="Selector.IsSelected" Value="True">
                    <Setter Property="TextElement.Foreground" Value="Blue"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

2
谢谢您的回答,但不幸的是,当ListBox失去焦点时,所选元素会变成灰色:(我希望在元素被选中但处于非活动状态时,前景为白色,背景为蓝色。 - Daniil Harik

5

对于我来说,这很有效 - 在活动和非活动的ListBox中,选择的项目的前景色和背景色是相同的。

<ListBox.ItemContainerStyle>
  <Style TargetType="{x:Type ListBoxItem}">
    <Style.Resources>
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="DodgerBlue"/>
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/>
      <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DodgerBlue"/>
      <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White"/>
    </Style.Resources>        
  </Style>
</ListBox.ItemContainerStyle>

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