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 的选择效果如鼠标移动到未选中项的效果或选择项的背景

先写一些简单的代码用于界面的绑定

  1. public partial class MainWindow : Window
  2. {
  3. public MainWindow()
  4. {
  5.  
  6. InitializeComponent();
  7. DataContext = this;
  8. Items = new List<Item> { new Item(1), new Item(2), new Item(3) };
  9. }
  10.  
  11. public List<Item> Items { get; set; }
  12. }
  13.  
  14. public class Item
  15. {
  16. public Item(int id)
  17. {
  18. Id = id;
  19. }
  20.  
  21. public int Id { get; set; }
  22. public string Text { get => $"This is Item number {Id}"; }
  23. }

在界面放一个 ListView 默认在鼠标移动到没有被选择的项的时候会出现背景

  1. <ListView ItemsSource="{Binding Items}">
  2. <ListView.ItemTemplate>
  3. <DataTemplate DataType="local:Item">
  4. <StackPanel>
  5. <TextBlock Text="{Binding Id}" />
  6. <TextBlock Text="{Binding Text}" />
  7. </StackPanel>
  8. </DataTemplate>
  9. </ListView.ItemTemplate>
  10. </ListView>

通过写样式在 ItemContainerStyle 可以让 ListView 的在鼠标移动到未选择项的特效的颜色修改

  1. <!-- set SelectedBackgroundColor to Transparent when you do not need the background in selected items -->
  2. <Color x:Key="SelectedBackgroundColor">#00FFFFFF</Color>
  3. <Color x:Key="SelectedUnfocusedColor">#FFB2A3A2</Color>
  4.  
  5. <!-- set the MouseOverColor to Transparent when you do not need the effect in the unselected items -->
  6. <Color x:Key="MouseOverColor" >#00FFFFFF</Color>
  7.  
  8. <Style x:Key="ListViewItemStyle"
  9. TargetType="ListViewItem">
  10. <Setter Property="SnapsToDevicePixels"
  11. Value="true" />
  12. <Setter Property="OverridesDefaultStyle"
  13. Value="true" />
  14. <Setter Property="Template">
  15. <Setter.Value>
  16. <ControlTemplate TargetType="ListBoxItem">
  17. <Border x:Name="Border"
  18. Padding="2"
  19. SnapsToDevicePixels="true"
  20. Background="Transparent">
  21. <VisualStateManager.VisualStateGroups>
  22. <VisualStateGroup x:Name="CommonStates">
  23. <VisualState x:Name="Normal" />
  24. <VisualState x:Name="MouseOver" >
  25. <Storyboard>
  26. <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
  27. Storyboard.TargetProperty="(Panel.Background).
  28. (SolidColorBrush.Color)">
  29. <EasingColorKeyFrame KeyTime="0"
  30. Value="{StaticResource MouseOverColor}" />
  31. </ColorAnimationUsingKeyFrames>
  32. </Storyboard>
  33. </VisualState>
  34. <VisualState x:Name="Disabled" />
  35. </VisualStateGroup>
  36. <VisualStateGroup x:Name="SelectionStates">
  37. <VisualState x:Name="Unselected" />
  38. <VisualState x:Name="Selected">
  39. <Storyboard>
  40. <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
  41. Storyboard.TargetProperty="(Panel.Background).
  42. (SolidColorBrush.Color)">
  43. <EasingColorKeyFrame KeyTime="0"
  44. Value="{StaticResource SelectedBackgroundColor}" />
  45. </ColorAnimationUsingKeyFrames>
  46. </Storyboard>
  47. </VisualState>
  48. <VisualState x:Name="SelectedUnfocused">
  49. <Storyboard>
  50. <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
  51. Storyboard.TargetProperty="(Panel.Background).
  52. (SolidColorBrush.Color)">
  53. <EasingColorKeyFrame KeyTime="0"
  54. Value="{StaticResource SelectedUnfocusedColor}" />
  55. </ColorAnimationUsingKeyFrames>
  56. </Storyboard>
  57. </VisualState>
  58. </VisualStateGroup>
  59. </VisualStateManager.VisualStateGroups>
  60. <ContentPresenter />
  61. </Border>
  62. </ControlTemplate>
  63. </Setter.Value>
  64. </Setter>
  65. </Style>

可以直接使用上面的代码,如果想要让用户看不到鼠标移动到未选中项的特效可以设置 MouseOverColor 为透明,通过设置 SelectedBackgroundColor 可以让选中项的背景修改

因为颜色在 WPF 使用 #AARRBBGG 表示,如上面代码设置了 #00FFFFFF 就是透明,因为第一个 Alpha 为 0 也就是透明

在 ListView 使用刚才写的样式,运行代码可以看到下面图片

  1. <ListView ItemsSource="{Binding Items}"
  2. ItemContainerStyle="{StaticResource ListViewItemStyle}">
  3. <ListView.ItemTemplate>
  4. <DataTemplate DataType="local:Item">
  5. <StackPanel>
  6. <TextBlock Text="{Binding Id}" />
  7. <TextBlock Text="{Binding Text}" />
  8. </StackPanel>
  9. </DataTemplate>
  10. </ListView.ItemTemplate>
  11. </ListView>

https://stackoverflow.com/a/53557393/6116637

2018-12-1-WPF-修改-ItemContainerStyle-鼠标移动到未选中项效果和选中项背景的更多相关文章

  1. WPF中当鼠标移到按钮上时,按钮的背景图片消失的问题

    如果给按钮设置了背景图片,当鼠标移到按钮上的时候,按钮就好变成一个浅蓝色的按钮,背景图片就消失了,对于这个问题有很多解决方法,我只分享一下我的解决方法. 我第一次用的方式是在按钮中添加一个图片,不用背 ...

  2. 微信小程序 - 接口更新记录以及解决方案(2018/12/26)

    2018/8/17 - 背景音频需要在app.json添加requireBackGroundModes 2018/9/12 - 微信更改获取用户信息接口/获取位置等接口 - button 2018/1 ...

  3. 2018.12.02 Socket编程之初识Socket

    Socket编程主要分为TCP/UDP/SCTP三种,每一种都有各自的优点,所以会根据实际情况决定选用何种Socket,今天开始我将会逐步学习Socket编程,并将学习过程记录于此. 今天学习的是TC ...

  4. OPPO Developers Conference(2018.12.26)

    时间:2018.12.26地点:北京国家会议中心

  5. Tencent Cloud Developers Conference(2018.12.15)

    时间:2018.12.15地点:北京朝阳悠唐皇冠假日酒店

  6. 2018.12.1 Test

    目录 2018.12.1 Test A 串string(思路) B 变量variable(最小割ISAP) C 取石子stone(思路 博弈) 考试代码 B C 2018.12.1 Test 题目为2 ...

  7. 「版本升级」MyEclipse CI 2018.12.0正式发布

    新版本MyEclipse为WildFly 14新增一个新的服务器连接器,改进性能并新增一些Java 10修复程序.新版本为IDE做了几个核心修复,这是MyEclipse 2018一个更棒的升级. [M ...

  8. 调试大叔V2.1.0(2018.12.17)|http/s接口调试、数据分析程序员辅助开发神器

    2018.12.17 - 调试大叔 V2.1.0*升级http通讯协议版本,完美解决Set-Cookie引起的系列问题:*新增Content-Type编码格式参数,支持保存(解决模拟不同网站或手机请求 ...

  9. kali linux 2018.2 mysql密码修改后无效,外部无法连接问题。

    kali linux 2018.2 mysql密码修改后无效,外部无法连接问题 Kali Linux 2018.2 默认MySQL数据库是mariadb,可能和MySQL有些细微的变化,只需要做如下处 ...

  10. Wpf修改控制的大小

    Wpf修改控制的大小 随窗体的改变而改变 在WINFORM中设置控件的Anchor属性就行了 在WPF中没有Anchor属性 但可以在布局中设置 相关属性实现同样的效果 相关属性 Horizontal ...

随机推荐

  1. Leetcode刷题——007.整数反转

    上代码: #include <cmath> class Solution { public: int reverse(int x) { ; long long tx=llabs(x); ) ...

  2. Nginx 配置参数

    1 Proxy_send_timeout 定义后端在多久的时间内必须返回完所有的数据给Nginx. 2 Proxy_read_timeout

  3. LaTex 插入图像,以及应用表格

    插入图像 参考:http://www.ctex.org/documents/latex/graphics/ 1: \includegraphics[width=20mm]{head.png} 应用表格 ...

  4. 『Golang』—— 标准库之 time

    ... package main import ( "fmt" "time" ) func main() { time.AfterFunc(time.Milli ...

  5. ActionContext 与 ServletActionContext获取Session的异同

    1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息, ...

  6. 567. Permutation in String【滑动窗口】

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

  7. 2018湘潭大学程序设计竞赛【E】

    题目链接:https://www.nowcoder.com/acm/contest/105/E 题意:给你美食种类和查询次数,告诉你美味度和价格,给你固定钱数,问你最多能吃到多少美味度的食物.(X真是 ...

  8. Ansible随笔8

    自定义模块的开发模式 1.决定自定义模块的存放路径 编辑/etc/ansible/ansible.cfg文件,修改library = /usr/share/ansible/. 这样就告诉ansible ...

  9. tomcat+apache+jk

    安装JDK下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html安装 rpm ...

  10. svg圆环缓冲动画

    代码如下 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...