首页 技术 正文
技术 2022年11月7日
0 收藏 443 点赞 324 浏览 2889 个字

原文: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.完成。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,108
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,202
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,837
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,920