(下图:进行多项选择的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>