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

Dan*_*rik 4 .net wpf listbox listboxitem

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

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

WPF ListView非活动选择颜色

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

谢谢

Ken*_*art 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>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢您的回答,但是不幸的是,当ListBox失去焦点时,选定的元素变为灰色:(当选定元素但不活动时,我会将前景变为白色,将背景变为蓝色。 (2认同)

Jan*_*sek 5

对我来说,这可行-在活动和非活动列表框中,所选项目的前景和背景颜色都相同。

<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>
Run Code Online (Sandbox Code Playgroud)