【问题标题】:WPF ListView Inactive Selection Color and element font colorWPF ListView 非活动选择颜色和元素字体颜色
【发布时间】:2009-04-27 15:05:19
【问题描述】:

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

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

WPF ListView Inactive Selection Color

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

谢谢

【问题讨论】:

    标签: .net wpf listbox listboxitem


    【解决方案1】:

    不幸的是,您不能使用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>
    

    【讨论】:

    • 感谢您的回答,但不幸的是,当 ListBox 失去焦点时,所选元素变为灰色 :( 当元素被选中但处于非活动状态时,我将前景为白色,背景为蓝色。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2010-09-27
      • 2019-01-17
      • 2011-06-09
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 2016-08-24
      • 1970-01-01
      相关资源
      最近更新 更多