WPF ListView Inactive Selection Color and element font color

WPF ListView Inactive Selection Color and element font color

千纸鹤带着心事 发布于 2021-11-27 字数 375 浏览 755 回复 2 原文

I'm able to set ListView Inactive Selection Color

I used solution described in following question

WPF ListView Inactive Selection Color

I need to change font color of selected inactive element, is there easy way to accomplish this?

Thank You

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝礼 2022-06-07 2 楼

For me this worked - in both active and inactive ListBox, the Foreground and Background color of selected itemss are the same.

<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>
伴随着你 2022-06-07 1 楼

Unfortunately, you can't use SystemColors.ControlTextBrushKey because it applies when the item is unselected, or when it is selected but inactive (your question reads as though you're only interested in the latter). However, you can do this:

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