WPF ListView Inactive Selection Font Color and Element Color

I can set the color of inactive ListView selection

I used the solution described in the next question

Inactive WPF ListView Selection Color

I need to change the font color of a selected inactive element, is there an easy way to do this?

thanks

+3
source share
2 answers

Unfortunately, you cannot use SystemColors.ControlTextBrushKeyit because it is applied when the item is not selected, or when it is selected but inactive (your question is read as if you are only interested in the last one). 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>
+6
source

- , 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>
+3

Source: https://habr.com/ru/post/1707194/


All Articles