如何以交替背景突出显示WPF Listview控件中的选定行 [英] How to highlight selected row in WPF Listview control with alternating background

查看:46
本文介绍了如何以交替背景突出显示WPF Listview控件中的选定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将VS 2008与.NET Framework 3.5一起使用.我已设置代码以替换WPF ListView控件中的背景行.使用的一种颜色是白色.使用的另一种颜色是浅绿色.单击白线时,它会以浅蓝色突出显示该线.单击绿线时,没有突出显示,背景颜色保持浅绿色.我尝试在XAML中指定HighlightBrushKey和ControlBrushKey,但它们没有任何效果.我该怎么做才能在单击时突出显示绿线?

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?

这是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事件,但是不知道如何获取正确的属性或查看元素以更改该行的背景颜色:

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:

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

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

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

推荐答案

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

这篇关于如何以交替背景突出显示WPF Listview控件中的选定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆