WPF ListView设置单选多选样式

本文介绍了如何在WPF中为ListView设置选中、焦点、鼠标悬停及失去焦点时的颜色样式,以实现单选或多选时的视觉效果。通过使用StyleSnooper工具,可以轻松查看并自定义这些WPF样式的细节。

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>
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值