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

时间:2019-07-11 14:25:21

标签: c# wpf listview listviewitem

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

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

1 个答案:

答案 0 :(得分:2)

您必须覆盖ControlTemplate中的ListViewItem。这样,您还可以将突出显示任何其他视觉用户交互行为的内容分开设计,并创建过渡动画。

<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

为过渡设置动画