如何使用交替背景突出显示 WPF Listview 控件中的选定行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15776470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:26:45  来源:igfitidea点击:

How to highlight selected row in WPF Listview control with alternating background

c#wpfxamlvisual-studio-2008listview

提问by Bob Bryan

I am using VS 2008 with 3.5 of the .NET framework. I have set up my code to alternate the background lines in a WPF ListView control. One color used is white. The other color used is a light shade of green. When a white line is clicked on, it highlights the line in light blue. When a green line is clicked on, there is no highlighting and the background color remains light green. I have tried specifying the HighlightBrushKey and the ControlBrushKey in XAML, but they had no effect. What do I need to do in order for the green lines to be highlighted when clicked on?

我使用 VS 2008 和 3.5 的 .NET 框架。我已经设置了我的代码来交替 WPF ListView 控件中的背景行。使用的一种颜色是白色。使用的另一种颜色是浅绿色。当单击一条白线时,它会以浅蓝色突出显示该线。单击绿线时,没有突出显示,背景颜色保持浅绿色。我曾尝试在 XAML 中指定 HighlightBrushKey 和 ControlBrushKey,但它们没有效果。我需要做什么才能在单击时突出显示绿线?

Here is the XAML code:

这是 XAML 代码:

<!-- Define the resource for the alternating background background used in the ListView objects.  -->
<StackPanel.Resources>
   <Style x:Key="alternatingListViewItemStyle" TargetType="{x:Type ListViewItem}">
      <Style.Resources>
         <!-- Foreground for Selected ListViewItem -->
         <!-- <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/> -->
         <!-- Background for Selected ListViewItem -->
         <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/>
         <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Brown"/>
      </Style.Resources>
      <Style.Triggers>
         <!-- Tried setting the background property here, but this did not work.
         <DataTrigger Binding="{Binding Path=RowSelected}" Value="True">
            <Setter Property="Background" Value="Gainsboro"  />
            <Setter Property="FontWeight" Value="Bold" />
         </DataTrigger> -->
         <!-- setting up triggers for alternate background colors -->
         <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="#FFD9F2BF"></Setter>
         </Trigger>
         <!-- setting up triggers for alternate background colors
         <Trigger Property="ItemsControl" Value="1">
            <Setter Property="Background" Value="#FFD9F2BF"></Setter>
         </Trigger> -->
         <Trigger Property="ItemsControl.AlternationIndex" Value="2">
            <Setter Property="Background" Value="White"></Setter>
         </Trigger>
      </Style.Triggers>
      <!-- setting row height here -->
   </Style>
</StackPanel.Resources>

<ListView x:Name="ListView1" ItemContainerStyle="{StaticResource alternatingListViewItemStyle}"
AlternationCount="2" SelectionChanged="ListView1_SelectionChanged" SelectionMode="Multiple"
HorizontalAlignment="Left" ItemsSource = "{Binding ElementName=LobbyWindow, Path=ListCollection1}">
   <ListView.View>
      <GridView>
         <GridViewColumn DisplayMemberBinding="{Binding Game}">
         <GridViewColumnHeader Content="Game" FontWeight="Bold" />
         </GridViewColumn>
         <GridViewColumn DisplayMemberBinding="{Binding Stakes}">
         <GridViewColumnHeader Content="Stakes" Width="68" FontWeight="Bold" />
         </GridViewColumn>
         <GridViewColumn Width="30" DisplayMemberBinding="{Binding Seats}">
         <GridViewColumnHeader Content="Seats" FontWeight="Bold" />
         </GridViewColumn>
      </GridView>
   </ListView.View>
</ListView>

I also have a Selection_Changed event defined in ListView1, but don't know how to grab the correct property or view element out in order to change the background color for that line:

我也在 ListView1 中定义了一个 Selection_Changed 事件,但不知道如何获取正确的属性或视图元素以更改该行的背景颜色:

private void ListView1_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
   ListView listview = sender as ListView;
}

Any suggestions in XAML or C# code behind would be appreciated.

任何在 XAML 或 C# 代码后面的建议将不胜感激。

回答by RonakThakkar

<StackPanel.Resources>
   <Style x:Key="alternatingListViewItemStyle"
          TargetType="{x:Type ListViewItem}">
      <Style.Triggers>
         <Trigger Property="ItemsControl.AlternationIndex" Value="0">
            <Setter Property="Background" Value="#FFD9F2BF"/>
         </Trigger>
         <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="White"/>
         </Trigger>
         <MultiTrigger>
            <MultiTrigger.Conditions>
               <Condition Property="IsSelected" Value="True"/>
               <Condition Property="ItemsControl.AlternationIndex" Value="0"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" Value="LightGreen"/>
         </MultiTrigger>
         <MultiTrigger>
            <MultiTrigger.Conditions>
               <Condition Property="IsSelected" Value="True"/>
               <Condition Property="ItemsControl.AlternationIndex"
                          Value="1"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" Value="LightBlue"/>
         </MultiTrigger>
      </Style.Triggers>
   </Style>
</StackPanel.Resources>