WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画
原文:WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画
利用WPF的ListView控件实现类似于Winform中DataGrid行背景色交替变换的效果,同时增加鼠标的悬停效果。
1.本文实现的效果如下:
2.所有的效果,我通过C#代码实现。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.ComponentModel;
using System.Windows.Media.Animation;
namespace BarCodeSystem
{
public class ListViewItemStyleSelector:StyleSelector
{
private Dictionary> storyboards = new Dictionary>();
///
/// 下面的示例演示如何定义一个为行定义 Style 的 StyleSelector。
/// 此示例依据行索引定义 Background 颜色,为每行定义ListViewItem的动画板(Storyboard)。
///ListView控件在初始化的时候,每初始化一行ListViewItem的时候都会进入该函数
///
///
///
///
public override Style SelectStyle(object item, DependencyObject container)
{
Style st = new Style();
st.TargetType=typeof(ListViewItem);
Setter backGroundSetter = new Setter();
backGroundSetter.Property = ListViewItem.BackgroundProperty;
ListView listview =
ItemsControl.ItemsControlFromItemContainer(container)
as ListView;//获得当前ListView
int index =
listview.ItemContainerGenerator.IndexFromContainer(container);//行索引
if (index % 2 == 0)
{
backGroundSetter.Value = Brushes.AliceBlue;
}
else
{
backGroundSetter.Value = Brushes.Transparent;
}
st.Setters.Add(backGroundSetter);
//获得当前ListViewItem
ListViewItem iteml = (ListViewItem)listview.ItemContainerGenerator.ContainerFromIndex(index);
//故事板列表,用来存放1.鼠标进入故事板2.鼠标离开故事板
List sbl = new List();
//声明故事板
Storyboard storyboard = new Storyboard();
//1.鼠标进入故事板
//声明动画
DoubleAnimation fontEnterAnimation = new DoubleAnimation();
//动画的目标值
fontEnterAnimation.To = 16;
//开始之前的等待时间,设置0.5s的等待时间是为了模拟“悬停时间”
fontEnterAnimation.BeginTime = TimeSpan.FromSeconds(0.5);
//动画持续时间
fontEnterAnimation.Duration = TimeSpan.FromSeconds(1);
//动画缓动,可要可不要
fontEnterAnimation.EasingFunction = new ElasticEase() { EasingMode=EasingMode.EaseOut};
//绑定动画目标控件
Storyboard.SetTarget(fontEnterAnimation, iteml);
//绑定动画目标属性
Storyboard.SetTargetProperty(fontEnterAnimation, new PropertyPath("FontSize"));
//将动画板添加到故事板中
storyboard.Children.Add(fontEnterAnimation);
sbl.Add(storyboard);
//2.鼠标离开故事板
storyboard = new Storyboard();
DoubleAnimation fontLeaveAnimation = new DoubleAnimation();
fontLeaveAnimation.BeginTime = TimeSpan.FromSeconds(0);
fontLeaveAnimation.Duration = TimeSpan.FromSeconds(0.5);
Storyboard.SetTarget(fontLeaveAnimation, iteml);
Storyboard.SetTargetProperty(fontLeaveAnimation, new PropertyPath("FontSize"));
storyboard.Children.Add(fontLeaveAnimation);
sbl.Add(storyboard);
storyboards.Add(iteml, sbl);
//绑定鼠标进入事件
iteml.MouseEnter += new System.Windows.Input.MouseEventHandler(iteml_MouseEnter);
//绑定鼠标离开事件
iteml.MouseLeave += new System.Windows.Input.MouseEventHandler(iteml_MouseLeave);
return st;
}
///
/// 鼠标进入事件
///
///
///
private void iteml_MouseEnter(object sender, RoutedEventArgs e)
{
ListViewItem item=(ListViewItem)sender;
List storyboard = storyboards[item];
storyboard[0].Begin();
}
private void iteml_MouseLeave(object sender, RoutedEventArgs e)
{
ListViewItem item = (ListViewItem)sender;
List storyboard = storyboards[item];
storyboard[1].Begin();
}
}
}
3.Xaml文件中代码如下:
4.完成。
WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画的更多相关文章
- WPF常用控件应用demo
WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...
- C# winfrom ListView控件实现自由设置每一行字体及背景色等
背景:公司经常会需要将日志信息,输出到一个对话框中显示出来.之前一直采用的listbox控件,操作简单,使用方便,但是遗憾的是,不能自由控制每一行的状态. 于是想了如下几个方案: (1)重绘listb ...
- Android Material适配 为控件设置指定背景色和点击波纹效果
Android Material适配 为控件设置指定背景色和点击波纹效果,有需要的朋友可以参考下. 大部分时候,我们都需要为控件设置指定背景色和点击效果 4.x以下可以使用selector,5.0以上 ...
- 深入探讨WPF的ListView控件
接上一篇博客初步探讨WPF的ListView控件(涉及模板.查找子控件) 我们继续探讨ListView的用法 一.实现排序功能 需求是这样的:假如我们把学生的分数放入ListView,当我 ...
- C#中如何让ListView控件点击选中整行
将Listview控件的FullRowSelect属性置为True,当然Listview的View属性应该是Details. 2017年6月25日17:15:55
- 初步探讨WPF的ListView控件(涉及模板、查找子控件) - GavinJun
本文结合模板的应用初步介绍ListView的应用 一.Xaml中如何建立数据资源 大部分数据都会来自于后台代码,如何Xaml同样的建立数据源呢?比如建立一个学生List: 首先引入命名空间: xmln ...
- ListView控件--2016年12月9日
ListView属性 ListView 名称 说明 AccessKey 重写 WebControl.AccessKey 属性. 不支持将此属性设置 ListView 控件.(覆盖 WebContr ...
- ListView控件
打气筒工具:将R.layout.item_listview布局添加到相应的view控件里面 View view=LayoutInflater.from(ScondPro.this).inflate ...
- 创建 WPF 工具箱控件
创建 WPF 工具箱控件 WPF (Windows Presentation Framework) 工具箱控件模板允许您创建 WPF 控件,会自动添加到 工具箱 安装扩展的安装. 本主题演示如何使用模 ...
随机推荐
- php面试题9(看的时候就应该随手截图做笔记的)
php面试题9(看的时候就应该随手截图做笔记的) 一.总结 看的时候就应该随手截图做笔记的 二.php面试题9 一.选择题:1.下面哪个表达式不能将两个字符串$s1 和$s2 串联成一个单独的字符串? ...
- 清楚arp
转载:http://wscyza.blog.51cto.com/898495/728717/ linux系统下清空arp 缓存(清空arp表)方法 命令红色字体标记 系统初始arp环境 [ro ...
- tig install ncursesw
./configure --prefix=/home/xxx/.local/ --with-ncursesw make[xxx$ ldd src/tig linux-vdso.so.1 => ( ...
- web报表工具FineReport经常使用函数的使用方法总结(文本函数)
文本函数 CHAR CHAR(number):依据指定数字返回相应的字符.CHAR函数可将计算机其它类型的数字代码转换为字符. Number:用于指定字符的数字,介于1Number:用于指定字符的数字 ...
- [Angular] NgRx/effect, why to use it?
See the current implementaion of code, we have a smart component, and inside the smart component we ...
- 第六章:任务执行——Java并发编程实战
任务:通常是一些抽象的且离散的工作单元.大多数并发应用程序都是围绕"任务执行"来构造的,把程序的工作分给多个任务,可以简化程序的组织结构便于维护 一.在线程中执行任务 任务的独立性 ...
- Matlab Tricks(二十五)—— plot 属性
marker: 边缘:'MarkerEdgeColor', [],(RGB 配色) 填充:'MarkerFaceColor', [](RGB 配色)
- 76个值得你注意的erlang编程习惯
http://www.tuicool.com/articles/ZNzuyu 前言 学习Erlang的时候在书的留白处随手记录了一些东西,还有一些记录在了demo的注释里面,今天抽时间整理出来了一部分 ...
- TCP协议的一些认识及实践
http://www.2cto.com/net/201210/163047.html 一.简介 引用<TCP/IP详解-卷1>中的介绍,TCP与UDP使用相同的网络层(IP层),TCP却向 ...
- Oracle12c导入scott测试用户(转)
登入DBA用户 connect sys as sysdba; 创建scott用户 create user c##scott identified by tiger;--用户名前加c##,12c要求 授 ...