WPF ListView非活动select颜色

我正在创build一个WPF应用程序,其中有几个ListViewselect在一行(类似于iTunes浏览器)。 问题是默认的非活动select颜色太浅。 (见下文) 默认无效选择颜色(太亮)

我怎样才能改变这种颜色,所以我不活动的listview看起来像这样? (见下文) 无效和主动选择颜色相同

用像这样的Style覆盖默认的SystemColor:

 <Style TargetType="ListViewItem"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/> </Style.Resources> </Style> 

ListBox模板使用名为ControlBrush的系统颜色来设置不活动的高亮颜色。 因此,您可以覆盖该颜色:

 <ListBox> <ListBox.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}">Red</SolidColorBrush> </ListBox.Resources> </ListBox> 

更改SystemColors.ControlBrushKey不适合我,我不得不改变SystemColors.InactiveSelectionHighlightBrushKey

所以,而不是:

 <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red /> 

我不得不使用:

 <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/> 

答案在某些情况下可以解决问题,但不是理想的,因为当控件被禁用/只读时它会中断,而且也会覆盖配色scheme,而不是利用它们。 我的build议是在ListBox标签中添加以下内容:

 <ListBox....> <ListBox.Resources> <Style TargetType="ListBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Border Name="Border" Padding="2" SnapsToDevicePixels="true"> <ContentPresenter /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="true"> <Setter TargetName="Border" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.Resources> </ListBox> 

这将做什么是设置高亮背景颜色列表框项目select(无论控制状态)。

我的回答是基于已经给出的答案和以下博客的帮助: http : //blogs.vbcity.com/xtab/archive/2009/06/29/9344.aspx

您必须重写SystemColors的一些属性。 看看SystemColors类(MSDN) 。 有比InactiveSelectionHighlightBrushKey更多的属性,例如影响文本颜色的InactiveSelectionHighlightTextBrushKey。

 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Yellow"/> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="Blue"/> <Style TargetType="ListViewItem"> <Setter Property="FontSize" Value="20" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="Padding" Value="25,5" /> </Style> </Window.Resources> <StackPanel Orientation="Horizontal"> <ListView> <ListViewItem Content="Item" /> <ListViewItem Content="Item" /> </ListView> <ListView> <ListViewItem Content="Item" /> <ListViewItem Content="Item" /> </ListView> </StackPanel> </Window> 

在这里输入图像描述

对我来说,这个窍门是:

  <ListBox HorizontalContentAlignment="Stretch"> <ListBox.ItemTemplate> <DataTemplate> <Label Margin="-5, -2,-5,-2" Content="{Binding Item}"> <Label.Style> <Style TargetType="Label"> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}},Path=IsFocused}" Value="False"/> <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True"/> </MultiDataTrigger.Conditions> <Setter Property="Background" Value="CornflowerBlue"/> </MultiDataTrigger> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True"> <Setter Property="Foreground" Value="White"/> </DataTrigger> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="False"> <Setter Property="Foreground" Value="Black"/> </DataTrigger> </Style.Triggers> </Style> </Label.Style> </Label> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

在旧的.NET框架覆盖系统颜色不起作用。 在.NET Framework 4.0中工作的解决scheme就在这里 。

 <ListView> <ListView.Resources> <Style TargetType="{x:Type ListViewItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListViewItem}"> <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> </Border> <ControlTemplate.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Selector.IsSelectionActive" Value="False" /> <Condition Property="IsSelected" Value="True" /> </MultiTrigger.Conditions> <Setter Property="Background" TargetName="Bd" Value="DarkOrange" /> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Selector.IsSelectionActive" Value="True" /> <Condition Property="IsSelected" Value="True" /> </MultiTrigger.Conditions> <Setter Property="Background" TargetName="Bd" Value="OrangeRed" /> </MultiTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 

适用于ListBox和ListView。