0

我遇到了 WPF ListView 控件的问题,其中选定的项目颜色在 Windows 7(Aero)上不受尊重,但在 Server 2008 上有效。

Windows Server 2008 屏幕截图 Windows 7 / Aero 屏幕截图

第一张图片来自 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>
4

1 回答 1

1

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

相反,您可以创建一个Trigger侦听 的IsSelected属性ListViewItem并在其为真时更改颜色:

<Style TargetType="ListViewItem">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="DeepPink" />
        </Trigger>
    </Style.Triggers>
</Style>
于 2013-04-17T18:13:08.007 回答