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 控件,会自动添加到 工具箱 安装扩展的安装. 本主题演示如何使用模 ...
随机推荐
- Unity3D资源管理架构
在Unity3D引擎中,场景资源文件(.unity)是以2进制格式存储的.但同一时候它也有一种基于文本的表现格式. 可在Edit>Project Setting>Editor 中设置: 1 ...
- #import </usr/include/objc/objc-class.h> not such file or directory问题的解决方法
近期在使用一些开源的demo,打开后出现这个错误,然后能够把 #import </usr/include/objc/objc-class.h> 改动为以下 #import <objc ...
- C# 关于反射事件
在frmMain类中的代码 private void StartRun(string tag, string date, bool tipType) { var d ...
- SpringSecurity3.2.5自己定义角色及权限的教程
近期阴差阳错的搞上了SpringSecurity3.由于是自己做的小系统.中间遇到了非常多坑,基本每一个坑都踩过了,网上也查了不少资料,发现有不少错误的.更是让我绕了一圈又一圈,如今把一些主要的东西总 ...
- 5.3.3.1 deque其他使用方式
在本节里提供了一些关于deque其他使用方式. 提供相似UNIX中的命令tail的功能,显示一个文件最后面一段文本: def tail(filename, n=10): '返回文件最后的n行文本' w ...
- [Ramda] Get a List of Unique Values From Nested Arrays with Ramda (flatMap --> Chain)
In this lesson, we'll grab arrays of values from other arrays, resulting in a nested array. From the ...
- 【u226】查单词
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 全国英语四级考试就这样如期到来了.可是小Y依然没有做好充分的准备.为了能够大学毕业,可怜的小Y决定作弊 ...
- js 时间日期函数
忘记从哪里拷贝过来的了,侵删 Date.prototype.format = function (format) { var date = { "M+": this.getMont ...
- 【a202】&&【9208】输油管道问题
Time Limit: 10 second Memory Limit: 2 MB 问题描述 某石油公司计划建造一条由东向西的主输油管道.该管道要穿过一个有n 口油井的油 田.从每口油井都要有一条输油管 ...
- linux 分发同步脚本与分发命令脚本
同步脚本,在第5步要拼接自己配置的主机名 #!/bin/bash # 获取输入参数个数,如果没有参数,直接退出 pcount=$# )); then echo no args; exit; fi # ...