C# 使用交替项Win7在ListView上设置选定项的颜色

C# 使用交替项Win7在ListView上设置选定项的颜色,c#,.net,wpf,listview,C#,.net,Wpf,Listview,我对WPF ListView控件有一个问题,其中所选项目颜色在Windows 7(Aero)上不受尊重,但在Server 2008上工作 第一张图片来自Windows Server 2008计算机,单击了“Bacon”选项。然后我在Windows7上运行了相同的程序,并点击了“培根”选项 很明显,我为所选项目设置的背景在Aero主题上没有得到尊重,但我不知道如何处理它 XAML代码: <Window x:Class="WPFDriver.MainWindow" xmlns

我对WPF ListView控件有一个问题,其中所选项目颜色在Windows 7(Aero)上不受尊重,但在Server 2008上工作

第一张图片来自Windows Server 2008计算机,单击了“Bacon”选项。然后我在Windows7上运行了相同的程序,并点击了“培根”选项

很明显,我为所选项目设置的背景在Aero主题上没有得到尊重,但我不知道如何处理它

XAML代码:

<Window x:Class="WPFDriver.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="150" Width="150">
    <Window.Resources>
        <Style TargetType="ListViewItem">
            <Setter Property="BorderThickness" Value="0,1,0,0"/>
            <Setter Property="BorderBrush" Value="Gray" />
            <Style.Resources>
                <!-- set the selected item color -->
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="DeepPink"/>
            </Style.Resources>
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="{x:Static SystemColors.InfoBrush}" />
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="{x:Static SystemColors.WindowBrush}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <ListView Name="lvItems" AlternationCount="2" ItemsSource="{Binding Path=Joop}">
        <ListView.View>
            <GridView AllowsColumnReorder="True">
                <GridViewColumn Header="Widget" DisplayMemberBinding="{Binding Path=Widget}" ></GridViewColumn>
                <GridViewColumn Header="Coin" DisplayMemberBinding="{Binding Path=Coin}" ></GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Window>

代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace WPFDriver
{
    public partial class MainWindow : Window
    {
        public class Thing
        {
            public string Widget { get; set; }
            public string Coin { get; set; }
        }

        public ObservableCollection<Thing> Joop { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = this;
            Joop = new ObservableCollection<Thing>();

            Joop.Add(new Thing() { Widget = "Code", Coin = "Quarter" });
            Joop.Add(new Thing() { Widget = "Bicycle", Coin = "Nickel" });
            Joop.Add(new Thing() { Widget = "Bacon", Coin = "Dime" });
            Joop.Add(new Thing() { Widget = "A Koda", Coin = "Penny" });
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.Collections.ObjectModel;
名称空间WPFDriver
{
公共部分类主窗口:窗口
{
公共类事物
{
公共字符串小部件{get;set;}
公共字符串硬币{get;set;}
}
公共可观测集合Joop{get;set;}
公共主窗口()
{
初始化组件();
this.DataContext=this;
Joop=新的可观测集合();
Add(newthing(){Widget=“Code”,Coin=“Quarter”});
添加(newthing(){Widget=“Bicycle”,Coin=“Nickel”});
添加(newthing(){Widget=“Bacon”,Coin=“Dime”});
添加(newthing(){Widget=“A Koda”,Coin=“Penny”});
}
}
}
更新: Peter Hansen提出的第一个解决方案: 在ListView样式中,添加IsSelected和HighlightBrushKey

<Style TargetType="ListViewItem">
    <Setter Property="BorderThickness" Value="0,1,0,0"/>
    <Setter Property="BorderBrush" Value="Gray" />
    <Style.Resources>
        <!-- set the selected item color (Works for non Aero theme) -->
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="DeepPink"/>
    </Style.Resources>
    <Style.Triggers>
        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
            <Setter Property="Background" Value="{x:Static SystemColors.InfoBrush}" />
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="{x:Static SystemColors.WindowBrush}"/>
        </Trigger>
            <!-- set the selected item color (Works for Win7 Aero theme) -->
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="DeepPink"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

您不应该像那样重写
SystemColors.HighlightBrushKey
值,因为它不是真正可靠的。主题可能会以不同的方式获得用于选择的颜色,这就是您现在的体验

相反,您可以创建一个
触发器
,该触发器侦听
ListViewItem
IsSelected
属性,并在为真时更改颜色:

<Style TargetType="ListViewItem">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="DeepPink" />
        </Trigger>
    </Style.Triggers>
</Style>


这将Windows 7的背景色正确设置为粉红色,但系统将使用Server 2008的默认高亮显示颜色。我重新添加了
,除了这里显示的内容之外,我还有一个有效的解决方案。谢谢如果你有更好的办法在Server2008中修复它(非Aero主题),我洗耳恭听。你说得对。我已经浏览了我的一些代码,您必须同时执行这两项操作,才能使其适用于旧主题(在Windows XP、Server 2008等平台上)和新主题(在Windows Vista、7和8等平台上)。不幸的是,我认为没有更好的解决办法。