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

转载于:https://www.cnblogs.com/sjqq/p/6761885.html

自定义WPF ListBox的选择样式相关推荐

  1. WPF - Group分组对ListBox等列表样式的约束

    原文:WPF - Group分组对ListBox等列表样式的约束 在做WPF主题支持时,出现一个分组引起的莫名错误,可是折腾了我一番.在没有使用样式时,列表分组很正常,使用了别人写的ListBox列表 ...

  2. html下拉选择框箭头改为年,CSS自定义select下拉选择框的样式(不用其他标签模拟)...

    今天群里有人问到怎么自定义select下拉选择框的样式,于是群里就展开了激烈的讨论,刚开始一直就是考虑怎样使用纯CSS实现,把浏览器默认的样式覆盖掉,但最后均因兼容问题处理不好而失败告终,最后的解决方 ...

  3. 自定义html下拉选择框,CSS自定义select下拉选择框的样式(不用其他标签模拟)

    今天群里有人问到怎么自定义select下拉选择框的样式,于是群里就展开了激烈的讨论,刚开始一直就是考虑怎样使用纯CSS实现,把浏览器默认的样式覆盖掉,但最后均因兼容问题处理不好而失败告终,最后的解决方 ...

  4. [转] 使用模板自定义 WPF 控件

      [转] 使用模板自定义 WPF 控件                                                                                 ...

  5. html中legend样式,echarts自定义图例legend文字和样式

    话不多说,先上效果图. 要完成这个图并不难,主要是下面那个图例比较难,需要定制. 让我们从官方文档找找思路,官方文档关于legend.formatter是这样的:链接在这 难点在于: 1.这里的图例文 ...

  6. 自定义WPF窗体形状

    介绍 你好WPF爱好者. 随着WPF等统一API语言的发明,丰富用户界面变得非常容易. 创建丰富的用户界面只是一个想法. 您需要拥有的是创造性思维和最新技术融合. WPF和Expression Ble ...

  7. WPF QuickStart系列之样式和模板(Style and Template)

    原文:WPF QuickStart系列之样式和模板(Style and Template) 在WPF桌面程序中,当我们想构建一个统一的UI表现时(在不同操作系统下,显示效果一致),此时我们就需要使用到 ...

  8. 怎么修改谷歌浏览器文件提交按钮样式_使用css自定义input file浏览按钮样式

    前言 文件上传用途非常广泛,浏览器自带文件控件不太美观,很多时候不能满足需求,需要自定义input [type=file]的样式. 不同浏览器的file控件表现形式不一样 火狐 谷歌 IE 不同浏览器 ...

  9. 自定义 input[type=checkbox]的样式

    自定义 input[type="checkbox"]的样式 对复选框自定义样式,我们以前一直用的脚本来实现,不过现在可以使用新的伪类 :checkbox 来实现. 如果直接对复选框 ...

最新文章

  1. Kafka系列三 java API操作
  2. 初学者必学的C++项目!花3天搞定
  3. SourceChangeWarning:验证集上准确率很高,但是测试集上很低
  4. java concurrentmap原理_Java集合番外篇 -- ConcurrentHashMap底层实现和原理
  5. python PyQt5 QSlider类(滑块)
  6. boost::intrusive::get_parent_from_member用法的测试程序
  7. sqlserver数据库迁移mysql_在项目中迁移MS SQLServer到Mysql数据库,实现MySQL数据库的快速整合...
  8. FFmpeg深度学习模块的历史、现状和计划
  9. oracle数据库删除百万级数据库,数据库SQL优化大总结之 百万级数据库优化方案...
  10. 3.X内核下设备树–platform设备驱动
  11. Spark学习(一) -- Spark安装及简介
  12. 几道经典逻辑推理题,提高你的逻辑思考能力
  13. c++ opencv图像拼接
  14. 239页10万字“联、管、用”三位一体雪亮工程整体建设方案
  15. 利用向量组证明矩阵的秩不等式
  16. 三星s8是否支持html,三星Galaxy S8支持什么视频格式
  17. FFT——傅里叶变换
  18. Mesos-容器编排
  19. 利用lingo计算工厂生产最优解
  20. 客户干货分享| 哈啰出行:以人与技术的多方出击,提效移动应用研发

热门文章

  1. 在eclipse中安装jadclipse的反编译插件
  2. Infor与Marketo®合作,帮助全球企业实现客户体验变革
  3. python kafka
  4. Python 实现针对时间序列预测的特征选择
  5. onresize的应用--自适应弹窗
  6. MySQL Fabric集群功能整理---择录官网
  7. Introduction to Microservices
  8. Linux下xargs命令的使用
  9. VMware中CentOS设置静态IP
  10. Zend Framework实例教程三