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 ...
随机推荐
- Leetcode刷题——007.整数反转
上代码: #include <cmath> class Solution { public: int reverse(int x) { ; long long tx=llabs(x); ) ...
- Nginx 配置参数
1 Proxy_send_timeout 定义后端在多久的时间内必须返回完所有的数据给Nginx. 2 Proxy_read_timeout
- LaTex 插入图像,以及应用表格
插入图像 参考:http://www.ctex.org/documents/latex/graphics/ 1: \includegraphics[width=20mm]{head.png} 应用表格 ...
- 『Golang』—— 标准库之 time
... package main import ( "fmt" "time" ) func main() { time.AfterFunc(time.Milli ...
- ActionContext 与 ServletActionContext获取Session的异同
1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息, ...
- 567. Permutation in String【滑动窗口】
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- 2018湘潭大学程序设计竞赛【E】
题目链接:https://www.nowcoder.com/acm/contest/105/E 题意:给你美食种类和查询次数,告诉你美味度和价格,给你固定钱数,问你最多能吃到多少美味度的食物.(X真是 ...
- Ansible随笔8
自定义模块的开发模式 1.决定自定义模块的存放路径 编辑/etc/ansible/ansible.cfg文件,修改library = /usr/share/ansible/. 这样就告诉ansible ...
- tomcat+apache+jk
安装JDK下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html安装 rpm ...
- svg圆环缓冲动画
代码如下 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...