(下圖:進行多項選擇的ListBox)

首先介紹一種簡單地方法:就是通過自定義SystemColors類的參數來自定義WPF ListBox選擇顏色的,SystemColors的HighlightBrushKey和HighlightTextBrushKey分別代表ListBoxItem被選中時文字和背景顏色,沒有Highlight的BrushKey代表ListBox沒有焦點時的選中項文字和背景顏色:
1 <ListBox> 2 3 <ListBox.Resources> 4 5 <Style TargetType="ListBoxItem"> 6 7 <Style.Resources> 8
<!-- Background for Selected ListViewItem --> 9 <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Pink"/> 10 11 <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray"/> 12 <!-- Foreground for Selected ListViewItem --> 13 <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red"/> 14 15 <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Green"/> 16 17 </Style.Resources> 18 19 </Style> 20 21 </ListBox.Resources> 22 23 <ListBoxItem>AAA</ListBoxItem> 24 25 <ListBoxItem>B</ListBoxItem> 26 27 <ListBoxItem>ccc</ListBoxItem> 28 29 </ListBox>
這樣的話,ListBox選中顏色變成了這樣:

可是這種方法僅僅能改變統一的顏色,無法完成其他更多要求。
那么另一種更強大的方法就是在模板中定義。一種方法就是在控件模板中根據ListBoxItem的IsSelected屬性判斷是否被選中,然后利用WPF觸發器來設置被選中后的樣式。但是如果你的ListBox定義了數據模板的話你會發現數據模板是顯示在控件模板之上的,因此控件模板上的某些顯示元素會被數據模板蓋住,如果此類情況發生,那么只能在數據模板上添加選中后的元素設置。這里可以通過一個RelativeBinding = FindAncestor的綁定來尋找可視化樹中的ListBoxItem的IsSelected屬性來在數據模板中判斷ListBoxItem是否被選中。
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color=" Red"/>
</Style.Resources>
<Setter Property="Panel.Background" Value="#00FFFFFF"/>
<Setter Property="Control.HorizontalContentAlignment">
<Setter.Value>
<Binding Path="HorizontalContentAlignment"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}" />
</Setter.Value>
</Setter>
<Setter Property="Control.VerticalContentAlignment">
<Setter.Value>
<Binding Path="VerticalContentAlignment"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}" />
</Setter.Value>
</Setter>
<Setter Property="Control.Padding" Value="2,0,0,0"/>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border BorderThickness="{TemplateBinding Border.BorderThickness}"
Padding="{TemplateBinding Control.Padding}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}"
Name="Bd"
SnapsToDevicePixels="True">
<ContentPresenter
Content="{TemplateBinding ContentControl.Content}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Selector.IsSelected" Value="True">
<Setter Property="Panel.Background" TargetName="Bd">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" />
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" />
</Setter.Value>
</Setter>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelected" Value="True"/>
<Condition Property="Selector.IsSelectionActive" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="Panel.Background" TargetName="Bd">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" />
</Setter.Value>
</Setter>
</MultiTrigger>
<Trigger Property="UIElement.IsEnabled" Value="False">
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="UIElement.IsMouseOver" Value="True">
<Setter Property="Panel.Background" TargetName="Bd" Value="Blue">
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListView Margin="48,22,110,0" Name="listView1" Height="100" VerticalAlignment="Top">
<ListView.View>
<GridView>
<GridViewColumn Header="12"/>
<GridViewColumn Header="12"/>
<GridViewColumn Header="12"/>
</GridView>
</ListView.View>
<ListViewItem>123</ListViewItem>
<ListViewItem>123</ListViewItem>
<ListViewItem>123</ListViewItem>
</ListView>
<TextBox Height="23" Margin="94,0,64,67" Name="textBox1" VerticalAlignment="Bottom" />
</Grid>
</Window>
