如何在 WPF 中更改 listviewitems 突出显示的颜色

How to change a listviewitems highlighted colour in WPF

我只是想更改 WPF 中 listviewitem 的突出显示颜色。我在网上找到的解决方案(包括 Whosebug)对我的列表视图没有影响。我要疯了吗?我错过了什么吗?请告诉我如何做到这一点。这是我的示例代码。这些项目仍然显示默认的蓝色。

<Window.Resources>
    <Style TargetType="ListViewItem">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static 
SystemColors.HighlightBrushKey}" Color="Red" />
        </Style.Resources>
    </Style>
</Window.Resources>

<Grid>
    <ListView VerticalAlignment="Top" Background="#2e2e2e" 
Foreground="White">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static 
SystemColors.HighlightTextBrushKey}"
                             Color="Red" />

                    <SolidColorBrush x:Key="{x:Static 
SystemColors.HighlightBrushKey}"
                             Color="Purple" />
                </Style.Resources>
            </Style>
        </ListView.ItemContainerStyle>

        <ListViewItem Content="ITEM" />
        <ListViewItem Content="ITEM" />
        <ListViewItem Content="ITEM" />
        <ListViewItem Content="ITEM" />
        <ListViewItem Content="ITEM" />
    </ListView>

    <Button VerticalAlignment="Bottom" Height="50" />
 </Grid>

您必须覆盖 ListViewItemControlTemplate。通过这种方式,您还可以通过突出显示任何其他视觉用户交互行为来进行设计,并创建过渡动画。

<Style TargetType="ListBoxItem">
  <Style.Resources>
    <SolidColorBrush x:Key="HighlightTextBrushKey"
                     Color="Red" />
    <SolidColorBrush x:Key="HighlightBrushKey"
                     Color="Purple" />
    <SolidColorBrush x:Key="HighlightMouseOverBrushKey"
                     Color="{Binding Source={StaticResource HighlightBrushKey}, Path=Color}" 
                     Opacity="0.3" />
  </Style.Resources>

  <Style.Setters>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="ListBoxItem">
          <Border BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}"
                  Background="{TemplateBinding Background}" 
                  Padding="{TemplateBinding Padding}"
                  Margin="{TemplateBinding Margin}">
            <ContentPresenter />
          </Border>

          <ControlTemplate.Triggers>
            <Trigger Property="IsSelected" Value="True">
              <Setter Property="Background" Value="{StaticResource HighlightBrushKey}"/>
              <Setter Property="Foreground" Value="{StaticResource HighlightTextBrushKey}"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter Property="Background" Value="{StaticResource HighlightMouseOverBrushKey}"/>
              <Setter Property="Foreground" Value="{StaticResource HighlightTextBrushKey}"/>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style.Setters>
</Style>

触发器的替代方法,您可以在 VisualStateManager

的帮助下设置动画转换