0
我有显示所选的项目颜色未在Windows 7(航空)尊重的WPF ListView控件的问题,但工程服务器2008年组选定的项目色的ListView与交流项目的Win7
第一张照片来自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" });
}
}
}
UPDATE: 由彼得·汉森提出的解决方案一: 在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>
这正确地设置了Windows 7的粉红色的背景颜色,但系统使用Server 2008的默认高亮颜色。我添加了 '除了你在这里展示的内容外,我还有一个工作解决方案。谢谢。如果你有更好的想法来修复它在Server 2008(非航空主题),我全部耳朵。 –
K0D4
你说得对。我已经浏览了一些我的代码,并且必须同时执行这两个操作才能使其适用于较旧的主题(在Windows XP,Server 2008等)和新主题(在Windows Vista,7和8等)。不幸的是,我不认为有更好的解决方案。 –