使用交替项目Win7在ListView上设置所选项目颜色

时间:2013-04-17 17:52:49

标签: c# .net wpf listview

我遇到了WPF ListView控件的问题,其中所选的项目颜色在Windows 7(Aero)上未得到遵守,但适用于Server 2008。

Windows Server 2008 Screen Shot Windows 7 / Aero Screen Shot

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

很明显,我为所选项目设置的背景在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" });
        }
    }
}

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

1 个答案:

答案 0 :(得分:1)

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

相反,您可以创建一个Trigger来监听IsSelected的{​​{1}}属性,并在其为真时更改颜色:

ListViewItem