WPF中ListView如何改变选中条背景颜色

简介: 先上图 解决方法: ...   解决问题 在CSDN上找到另外一种方法: 用StyleSnooper看一下默认的Style,改一下就可以了。

先上图

解决方法:

<ListView ...>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Resources>
                <!-- Foreground for Selected ListViewItem -->
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" 
                                 Color="Black"/>
                <!-- Background for Selected ListViewItem -->
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                                 Color="Transparent"/>
            </Style.Resources>
        </Style>
    </ListView.ItemContainerStyle>
    ...
</ListView>

 

解决问题

在CSDN上找到另外一种方法:

用StyleSnooper看一下默认的Style,改一下就可以了。

三种颜色 IsMouseOver = Blue
        Select & Focus = Red
        Select & UnFocus = Yellow

大家也可以尝试下:

<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>

 

 

目录
相关文章
|
前端开发 C#
【C#/WPF】ListView的MVVM例子,及禁止拖动ListView的头部Header
原文:【C#/WPF】ListView的MVVM例子,及禁止拖动ListView的头部Header 一个ListView的MVVM简单例子: ...
1789 0
|
数据处理 C# 索引
WPF技术之ListView控件
WPF ListView控件是一个用来显示集合数据的控件,它以表格形式呈现数据,并支持对数据进行排序、筛选和操作。
680 0
WPF-样式问题-处理ListBox、ListView子项内容全填充问题
WPF-样式问题-处理ListBox、ListView子项内容全填充问题
517 0
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
413 0
|
C# 索引 容器
WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画
原文:WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画 利用WPF的ListView控件实现类似于Winform中DataGrid行背景色交替变换的效果,同时增加鼠标的悬停效果。
2114 1
|
C#
WPF ListView展示层叠信息
原文:WPF ListView展示层叠信息 通常我们在ListView中展示一列同类数据,例如城市名称。不过可以对ListView的DataTemplate稍作修改,让其显示层叠信息。例如:需要在ListView中显示省份和省份对应的城市名称。
1238 0
|
前端开发 C#
WPF MVVM模式下实现ListView下拉显示更多内容
原文:WPF MVVM模式下实现ListView下拉显示更多内容 在手机App中,如果有一个展示信息的列表,通常会展示很少一部分,当用户滑动到列表底部时,再加载更多内容。这样有两个好处,提高程序性能,减少网络流量。
1687 0
|
C# Windows
WPF ItemsControl ListBox ListView比较
原文:WPF ItemsControl ListBox ListView比较 在进行列表信息展示时,WPF中提供多种列表可供选择。这篇博客将对WPF ItemsControl, ListBox, ListView进行比较。
1347 0
|
大数据 C#
利用WPF的ListView进行大数据量异步加载
原文:利用WPF的ListView进行大数据量异步加载      由于之前利用Winform的ListView进行大数据量加载的时候,诟病良多,所以今天试着用WPF的ListView来做了一下,结果没有让我失望,我将一个拥有43000行,510列的csv文件导入到了ListView中,总共耗时在10s左右,并且在导入的过程中,软件界面上的提示信息一直在提示当前导入了多少条。
1885 0
|
C#
WPF 获取 ListView DataTemplate 中控件值
原文:WPF 获取 ListView DataTemplate 中控件值 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.
1802 0