WPF ListView选中颜色设置(单选多选)
ListView选中某型Item时自动进行高亮显示,下面代码可支持设置Item选中的颜色、Item有焦点的颜色、鼠标在Item上的颜色、失去焦点的颜色。
wpf样式可通过StyleSnooper工具进行查看
<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 x:Key="listviewItemKey" TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="BrushFocus" Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}"/>
</Style.Resources>
<Setter Property="Margin" Value="15,10"/>
<Setter Property="Background" Value="LightGray"/>
<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="BrushFocus" />
</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.IsFocused" Value="False" />
<!--是否窗口处于激活状态-->
<Condition Property="Selector.IsSelectionActive" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="Panel.Background" TargetName="Bd">
<Setter.Value>
<!--<DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />-->
<DynamicResource ResourceKey="BrushLostFocus" />
</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 Grid.Row="0"
Name="ListViewProcess"
Cursor="Hand"
SelectionMode="Single"
ItemContainerStyle="{StaticResource listviewItemKey}" >
<!--<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="220" Height="50" Background="Transparent" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" FontSize="16" Foreground="Black" Margin=" 10 0 10 0" Text="{Binding ProcessName}" />
<Ellipse Grid.Column="1" Cursor="Hand" Width="30" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center">
<Ellipse.Fill>
<ImageBrush ImageSource="/Images/Home/流程.png" />
</Ellipse.Fill>
</Ellipse>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>-->
<ListViewItem>111</ListViewItem>
<ListViewItem>222</ListViewItem>
<ListViewItem>333</ListViewItem>
</ListView>
</Grid>
</Window>
本文介绍了如何在WPF中为ListView设置选中、焦点、鼠标悬停及失去焦点时的颜色样式,以实现单选或多选时的视觉效果。通过使用StyleSnooper工具,可以轻松查看并自定义这些WPF样式的细节。
1655





