WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画

简介: 原文:WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画 利用WPF的ListView控件实现类似于Winform中DataGrid行背景色交替变换的效果,同时增加鼠标的悬停效果。
原文: 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.完成。

目录
相关文章
|
7天前
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库
|
4月前
|
C#
浅谈WPF之装饰器实现控件锚点
使用过visio的都知道,在绘制流程图时,当选择或鼠标移动到控件时,都会在控件的四周出现锚点,以便于修改大小,移动位置,或连接线等,那此功能是如何实现的呢?在WPF开发中,想要在控件四周实现锚点,可以通过装饰器来实现,今天通过一个简单的小例子,简述如何在WPF开发中,应用装饰器,仅供学习分享使用,如有不足之处,还请指正。
59 1
|
8月前
|
C# Windows
WPF技术之图形系列Polygon控件
WPF Polygon是Windows Presentation Foundation (WPF)框架中的一个标记元素,用于绘制多边形形状。它可以通过设置多个点的坐标来定义多边形的形状,可以绘制任意复杂度的多边形。
449 0
|
8月前
|
C# Windows
WPF技术之RichTextBox控件
WPF RichTextBox是Windows Presentation Foundation (WPF)中提供的一个强大的文本编辑控件,它可以显示富文本格式的文本,支持多种文本处理操作。
343 0
|
4月前
|
前端开发 C# 容器
浅谈WPF之控件拖拽与拖动
使用过office的visio软件画图的小伙伴都知道,画图软件分为两部分,左侧图形库,存放各种图标,右侧是一个画布,将左侧图形库的图标控件拖拽到右侧画布,就会生成一个新的控件,并且可以自由拖动。那如何在WPF程序中,实现类似的功能呢?今天就以一个简单的小例子,简述如何在WPF中实现控件的拖拽和拖动,仅供学习分享使用,如有不足之处,还请指正。
105 2
|
8月前
|
数据挖掘 数据处理 C#
WPF技术之DataGrid控件
WPF DataGrid是一种可以显示和编辑数据的界面控件。它可以作为表格形式展示数据,支持添加、删除、修改、排序和分组操作。
173 0
|
8天前
|
C# 开发者 C++
一套开源、强大且美观的WPF UI控件库
一套开源、强大且美观的WPF UI控件库
125 0
|
5月前
|
算法 C# UED
浅谈WPF之控件模板和数据模板
WPF不仅支持传统的Windows Forms编程的用户界面和用户体验设计,同时还推出了以模板为核心的新一代设计理念。在WPF中,通过引入模板,将数据和算法的“内容”和“形式”进行解耦。模板主要分为两大类:数据模板【Data Template】和控件模板【Control Template】。
94 8
|
7月前
|
C#
WPF技术之动画系列-上下运动
本例子展现动画小球上下循环运动
125 0
|
8月前
|
定位技术 C# UED
WPF技术之ScrollViewer控件
WPF ScrollViewer是WPF中常用的一个控件,它提供了滚动视图的功能,可用于显示超出容器可视区域的内容。ScrollViewer通常用于容纳大量内容的控件,以在有限的空间内显示这些内容,并允许用户通过滚动来查看隐藏的部分。
686 0