【问题标题】:Set Selected item color on ListView with Alternating items Win7使用交替项目 Win7 在 ListView 上设置选定项目颜色
【发布时间】:2013-04-17 17:52:48
【问题描述】:

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

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

【问题讨论】:

    标签: c# .net wpf listview


    【解决方案1】:

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

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

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

    【讨论】:

    • 这正确地将 Windows 7 的背景颜色设置为粉红色,但系统正在使用 Server 2008 的默认突出显示颜色。我添加了 &lt;SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="DeepPink"/&gt; 以及您在此处显示的内容,我有一个可行的解决方案。谢谢。如果您有更好的想法在 Server 2008(非 Aero 主题)中修复它,我会全力以赴。
    • 你是对的。我查看了我的一些代码,你必须同时做这两个工作以使其适用于旧主题(在 Windows XP、Server 2008 等上)和新主题(在 Windows Vista、7 和 8 等上)。不幸的是,我认为没有更好的解决方案。
    猜你喜欢
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    相关资源
    最近更新 更多