2018-12-1-WPF-修改-ItemContainerStyle-鼠标移动到未选中项效果和选中项背景
| title | author | date | CreateTime | categories |
|---|---|---|---|---|
|
WPF 修改 ItemContainerStyle 鼠标移动到未选中项效果和选中项背景
|
lindexi
|
2018-12-01 08:18:33 +0800
|
2018-12-01 08:12:50 +0800
|
WPF
|
本文告诉大家如何通过修改 ItemContainerStyle 让 ListView 或 ListBox 的选择效果如鼠标移动到未选中项的效果或选择项的背景
先写一些简单的代码用于界面的绑定
public partial class MainWindow : Window
{
public MainWindow()
{ InitializeComponent();
DataContext = this;
Items = new List<Item> { new Item(1), new Item(2), new Item(3) };
} public List<Item> Items { get; set; }
} public class Item
{
public Item(int id)
{
Id = id;
} public int Id { get; set; }
public string Text { get => $"This is Item number {Id}"; }
}
在界面放一个 ListView 默认在鼠标移动到没有被选择的项的时候会出现背景
<ListView ItemsSource="{Binding Items}">
<ListView.ItemTemplate>
<DataTemplate DataType="local:Item">
<StackPanel>
<TextBlock Text="{Binding Id}" />
<TextBlock Text="{Binding Text}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
通过写样式在 ItemContainerStyle 可以让 ListView 的在鼠标移动到未选择项的特效的颜色修改
<!-- set SelectedBackgroundColor to Transparent when you do not need the background in selected items -->
<Color x:Key="SelectedBackgroundColor">#00FFFFFF</Color>
<Color x:Key="SelectedUnfocusedColor">#FFB2A3A2</Color> <!-- set the MouseOverColor to Transparent when you do not need the effect in the unselected items -->
<Color x:Key="MouseOverColor" >#00FFFFFF</Color> <Style x:Key="ListViewItemStyle"
TargetType="ListViewItem">
<Setter Property="SnapsToDevicePixels"
Value="true" />
<Setter Property="OverridesDefaultStyle"
Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="Border"
Padding="2"
SnapsToDevicePixels="true"
Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" >
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Panel.Background).
(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{StaticResource MouseOverColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled" />
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Panel.Background).
(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{StaticResource SelectedBackgroundColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Panel.Background).
(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{StaticResource SelectedUnfocusedColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
可以直接使用上面的代码,如果想要让用户看不到鼠标移动到未选中项的特效可以设置 MouseOverColor 为透明,通过设置 SelectedBackgroundColor 可以让选中项的背景修改
因为颜色在 WPF 使用 #AARRBBGG 表示,如上面代码设置了 #00FFFFFF 就是透明,因为第一个 Alpha 为 0 也就是透明
在 ListView 使用刚才写的样式,运行代码可以看到下面图片
<ListView ItemsSource="{Binding Items}"
ItemContainerStyle="{StaticResource ListViewItemStyle}">
<ListView.ItemTemplate>
<DataTemplate DataType="local:Item">
<StackPanel>
<TextBlock Text="{Binding Id}" />
<TextBlock Text="{Binding Text}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
2018-12-1-WPF-修改-ItemContainerStyle-鼠标移动到未选中项效果和选中项背景的更多相关文章
- WPF中当鼠标移到按钮上时,按钮的背景图片消失的问题
如果给按钮设置了背景图片,当鼠标移到按钮上的时候,按钮就好变成一个浅蓝色的按钮,背景图片就消失了,对于这个问题有很多解决方法,我只分享一下我的解决方法. 我第一次用的方式是在按钮中添加一个图片,不用背 ...
- 微信小程序 - 接口更新记录以及解决方案(2018/12/26)
2018/8/17 - 背景音频需要在app.json添加requireBackGroundModes 2018/9/12 - 微信更改获取用户信息接口/获取位置等接口 - button 2018/1 ...
- 2018.12.02 Socket编程之初识Socket
Socket编程主要分为TCP/UDP/SCTP三种,每一种都有各自的优点,所以会根据实际情况决定选用何种Socket,今天开始我将会逐步学习Socket编程,并将学习过程记录于此. 今天学习的是TC ...
- OPPO Developers Conference(2018.12.26)
时间:2018.12.26地点:北京国家会议中心
- Tencent Cloud Developers Conference(2018.12.15)
时间:2018.12.15地点:北京朝阳悠唐皇冠假日酒店
- 2018.12.1 Test
目录 2018.12.1 Test A 串string(思路) B 变量variable(最小割ISAP) C 取石子stone(思路 博弈) 考试代码 B C 2018.12.1 Test 题目为2 ...
- 「版本升级」MyEclipse CI 2018.12.0正式发布
新版本MyEclipse为WildFly 14新增一个新的服务器连接器,改进性能并新增一些Java 10修复程序.新版本为IDE做了几个核心修复,这是MyEclipse 2018一个更棒的升级. [M ...
- 调试大叔V2.1.0(2018.12.17)|http/s接口调试、数据分析程序员辅助开发神器
2018.12.17 - 调试大叔 V2.1.0*升级http通讯协议版本,完美解决Set-Cookie引起的系列问题:*新增Content-Type编码格式参数,支持保存(解决模拟不同网站或手机请求 ...
- kali linux 2018.2 mysql密码修改后无效,外部无法连接问题。
kali linux 2018.2 mysql密码修改后无效,外部无法连接问题 Kali Linux 2018.2 默认MySQL数据库是mariadb,可能和MySQL有些细微的变化,只需要做如下处 ...
- Wpf修改控制的大小
Wpf修改控制的大小 随窗体的改变而改变 在WINFORM中设置控件的Anchor属性就行了 在WPF中没有Anchor属性 但可以在布局中设置 相关属性实现同样的效果 相关属性 Horizontal ...
随机推荐
- 第48章 MDK的编译过程及文件类型全解
Frm: http://www.cnblogs.com/firege/p/5806134.html 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教 ...
- 自旋锁spinlock
1 在单处理器上的实现 单核系统上,不存在严格的并发,因此对资源的共享主要是多个任务分时运行造成的. 只要在某一时段,停止任务切换,并且关中断(对于用户态应用程序,不大可能与中断处理程序抢临界区资源) ...
- divide方法
java.math.BigDecimal.divide(BigDecimal divisor, RoundingMode roundingMode) 返回一个BigDecimal,其值为(this/除 ...
- nginx展示目录及美化
1.下载nginx 2.下载fancyindex git clone https://github.com/aperezdc/ngx-fancyindex.git ngx-fancyindex 3.下 ...
- JDK8新特性之重复注解
什么是重复注解 下面是JDK8中的重复注解(java.lang.annotation.Repeatable)定义的源码. @Documented @Retention(RetentionPolicy. ...
- 第四记 Java异常
Java异常结构图 Java所有异常都是从Throwable继承而来,Throwable有两个子类,Error与Exception. Error是错误,对于所有的编译时期的错误以及系统错误都是通过Er ...
- 第十二篇 requests模拟登陆知乎
了解http常见状态码 可以通过输入错误的密码来找到登陆知乎的post:url 把Headers拉到底部,可以看到form data _xsrf是需要发送的,需要发送给服务端,否则会返回403错误,提 ...
- 2019-8-31-dotnet-通过-HttpClient-下载文件同时报告进度的方法
title author date CreateTime categories dotnet 通过 HttpClient 下载文件同时报告进度的方法 lindexi 2019-08-31 16:55: ...
- Berry 指令设计
Berry 脚本源代码需要被编译为字节码指令流才能被 Berry 虚拟机执行.本文将详细地讲解 Berry 字节码指令(下面简称指令)的设计和实现.为了达到这个目的,本文由 3 部分构成:第 1 小节 ...
- Effective C++之条款1:视C++为一个语言联邦
C++中的sub-languages有如下四种: C Object-Oriented C++: (classes ,encapsulation(封装),inheritance(继承),polymo ...