如何在背景交替的WPF列表视图控件中突出显示选定行

本文关键字:控件 显示 视图 列表 背景 WPF | 更新日期: 2023-09-27 18:07:04

我使用的是带有3.5 . net框架的VS 2008。我已经设置了我的代码来替换WPF ListView控件中的背景线。使用的一种颜色是白色。另一种颜色是浅绿色。当点击一条白线时,它会用浅蓝色高亮显示这条线。当点击一条绿线时,没有高亮显示,背景颜色仍然是浅绿色。我已经尝试在XAML中指定HighlightBrushKey和ControlBrushKey,但它们没有效果。我需要做些什么才能使绿色线条在点击时突出显示?

以下是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>

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

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

如果有任何关于XAML或c#代码的建议,我将不胜感激。

如何在背景交替的WPF列表视图控件中突出显示选定行

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