此文章意在解决在WPF中ItemsControl类型的集合控件支持鼠标滚轮操作,并可控制滚动的速度。

第一步:给ItemsControl添加滚轮事件。

this.listBox.AddHandler(ListBox.MouseWheelEvent, new MouseWheelEventHandler(list_MouseWheel), true);

第二步:实现list_MouseWheel处理函数。

private void list_MouseWheel(object sender, MouseWheelEventArgs e)
{
            ItemsControl items = (ItemsControl)sender;
            ScrollViewer scroll = FindVisualChild<ScrollViewer>(items);
            scroll.PanningMode = PanningMode.HorizontalOnly;
            if (scroll != null)
            {
                int d = e.Delta;
                if (d > 0)
                {
                    this._HistoryListBox.ScrollSkip(-2);//此方法为集合控件的扩展方法。参数为控制滑动的速度。
                   // scroll.LineRight();
                }
                if (d < 0)
                {
                    //scroll.LineLeft();
                    this._HistoryListBox.ScrollSkip(2);
                }
                scroll.ScrollToTop();
            }
}

第三步:定义集合控件的扩展属性和方法。

  public static class ListBoxExtention
{
public static double GetHorizontalOffset(DependencyObject obj)
{
return (double)obj.GetValue(HorizontalOffsetProperty);
} public static void SetHorizontalOffset(DependencyObject obj, double value)
{
obj.SetValue(HorizontalOffsetProperty, value);
} /// <summary>
/// 动画属性,控制水平方向移动
/// </summary>
public static readonly DependencyProperty HorizontalOffsetProperty = DependencyProperty.RegisterAttached("HorizontalOffset", typeof(double), typeof(ListBoxExtention), new UIPropertyMetadata(0.0, OnHorizontalOffsetPropertyChanged)); static void OnHorizontalOffsetPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
((ScrollViewer)sender).ScrollToHorizontalOffset((double)args.NewValue);
} public static double GetVerticalOffset(DependencyObject obj)
{
return (double)obj.GetValue(VerticalOffsetProperty);
} public static void SetVerticalOffset(DependencyObject obj, double value)
{
obj.SetValue(VerticalOffsetProperty, value);
} // 用一个依赖属性作为verticaloffset存储器. 这使动画, 样式,绑定, 等等及其他
public static readonly DependencyProperty VerticalOffsetProperty =DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), typeof(ListBoxExtention), new UIPropertyMetadata(0.0, OnVerticalOffsetPropertyChanged)); static void OnVerticalOffsetPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
((ScrollViewer)sender).ScrollToVerticalOffset((double)args.NewValue); public static void ScrollToSelectedItem(this ListBox listbox, object selectedItem)
{
ScrollViewer scrollHost = VisualTreeHelper.GetChild(listbox, ) as ScrollViewer;
for (DependencyObject obj2 = listbox; obj2 != null; obj2 = VisualTreeHelper.GetChild(obj2, ))
{
ScrollViewer viewer = obj2 as ScrollViewer;
if (viewer != null)
{
scrollHost = viewer;
break;
}
}
if (scrollHost != null && scrollHost.IsLoaded)
{
double fromValue = scrollHost.HorizontalOffset;
double toValue = 0.0;
if (selectedItem != null)
{
FrameworkElement visual = listbox.ItemContainerGenerator.ContainerFromItem(selectedItem) as FrameworkElement;
if (visual != null)
{
Vector vector = VisualTreeHelper.GetOffset(visual);
toValue = (visual.ActualWidth - scrollHost.ViewportWidth) / + vector.X;
}
}
DoubleAnimation animation = new DoubleAnimation(fromValue, toValue, new TimeSpan(, , , , ), FillBehavior.HoldEnd);
scrollHost.BeginAnimation(ListBoxExtention.HorizontalOffsetProperty, animation);
}
} public static void ScrollSkip(this ListBox listbox, int count)
{
ScrollViewer scrollHost = VisualTreeHelper.GetChild(listbox, ) as ScrollViewer;
for (DependencyObject obj2 = listbox; obj2 != null; obj2 = VisualTreeHelper.GetChild(obj2, ))
{
ScrollViewer viewer = obj2 as ScrollViewer;
if (viewer != null)
{
scrollHost = viewer;
break;
}
}
if (scrollHost != null && scrollHost.IsLoaded)
{
double fromHorizontalOffset = scrollHost.HorizontalOffset;
double toHorizontalOffset = 0.0;
double fromVerticalOffset = scrollHost.VerticalOffset;
double toVerticalOffset = 0.0;
toHorizontalOffset = (scrollHost.ExtentWidth / listbox.Items.Count) * count + fromHorizontalOffset;
toVerticalOffset = (scrollHost.ExtentHeight / listbox.Items.Count) * count + fromVerticalOffset; if (ScrollViewer.GetHorizontalScrollBarVisibility(listbox) != ScrollBarVisibility.Disabled)
{
DoubleAnimation animation = new DoubleAnimation(fromHorizontalOffset, toHorizontalOffset, new TimeSpan(, , , , ), FillBehavior.HoldEnd);
scrollHost.BeginAnimation(ListBoxExtention.HorizontalOffsetProperty, animation);
}
if (ScrollViewer.GetVerticalScrollBarVisibility(listbox) != ScrollBarVisibility.Disabled)
{
DoubleAnimation animation = new DoubleAnimation(fromVerticalOffset, toVerticalOffset, new TimeSpan(, , , , ), FillBehavior.HoldEnd);
scrollHost.BeginAnimation(ListBoxExtention.VerticalOffsetProperty, animation);
}
}
} public static void ScrollSkip_Double(this ListBox listbox, Double count)
{
ScrollViewer scrollHost = VisualTreeHelper.GetChild(listbox, ) as ScrollViewer;
for (DependencyObject obj2 = listbox; obj2 != null; obj2 = VisualTreeHelper.GetChild(obj2, ))
{
ScrollViewer viewer = obj2 as ScrollViewer;
if (viewer != null)
{
scrollHost = viewer;
break;
}
}
if (scrollHost != null && scrollHost.IsLoaded)
{
double fromHorizontalOffset = scrollHost.HorizontalOffset;
double toHorizontalOffset = 0.0;
double fromVerticalOffset = scrollHost.VerticalOffset;
double toVerticalOffset = 0.0;
toHorizontalOffset = (scrollHost.ExtentWidth / listbox.Items.Count) * count + fromHorizontalOffset;
toVerticalOffset = (scrollHost.ExtentHeight / listbox.Items.Count) * count + fromVerticalOffset; if (ScrollViewer.GetHorizontalScrollBarVisibility(listbox) != ScrollBarVisibility.Disabled)
{
DoubleAnimation animation = new DoubleAnimation(fromHorizontalOffset, toHorizontalOffset, new TimeSpan(, , , , ), FillBehavior.HoldEnd);
scrollHost.BeginAnimation(ListBoxExtention.HorizontalOffsetProperty, animation);
}
if (ScrollViewer.GetVerticalScrollBarVisibility(listbox) != ScrollBarVisibility.Disabled)
{
DoubleAnimation animation = new DoubleAnimation(fromVerticalOffset, toVerticalOffset, new TimeSpan(, , , , ), FillBehavior.HoldEnd);
scrollHost.BeginAnimation(ListBoxExtention.VerticalOffsetProperty, animation);
}
}
}
}

WPF ItemsControl 控件支持鼠标滚轮滑动的更多相关文章

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

    原文:WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画 利用WPF的ListView控件实现类似于Winform中DataGrid行背景色交替变换的效果,同 ...

  2. WPF 在image控件用鼠标拖拽出矩形

    原文:WPF 在image控件用鼠标拖拽出矩形 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee ...

  3. C# WPF 歌词控件(支持逐字定位描色效果)

    原文:C# WPF 歌词控件(支持逐字定位描色效果) 之前做了一个模仿网易云歌词的控件,实现了加载网易云歌词并能随音乐播放进度定位歌词.今天呢将在这个控件的基础上增加逐字定位描色功能,如下图效果(QQ ...

  4. WPF界面开发者注意啦!Scheduler控件支持时区功能了,你get了吗

    DevExpress广泛应用于ECM企业内容管理. 成本管控.进程监督.生产调度,在企业/政务信息化管理中占据一席重要之地.通过DevExpress WPF Controls,您能创建有着强大互动功能 ...

  5. 如何让DbGrid支持鼠标滚轮滚动

    如何让DbGrid支持鼠标滚轮滚动 在主窗体上加一个ApplicationEvents控件(控件在Additional面板中), 在它的OnMessage事件中加入下述代码,一切搞定-! proced ...

  6. WPF第三方控件盘点

    WPF统一的编程模型.语言和框架,实现了界面设计人员和开发人员工作可以分离的境界,鉴于WPF强大的优势,且一直是开发者关注的地方,下面和大家分享基于WPF项目开发需要用到的第三方控件,包括业界最受好评 ...

  7. 创建 WPF 工具箱控件

    创建 WPF 工具箱控件 WPF (Windows Presentation Framework) 工具箱控件模板允许您创建 WPF 控件,会自动添加到 工具箱 安装扩展的安装. 本主题演示如何使用模 ...

  8. WPF常用控件应用demo

    WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...

  9. 一款开源免费的WPF图表控件ModernuiCharts

    一款简洁好看的Chart控件  支持WPF.silverlight.Windows8  ,基本够用,主要是开源免费的.(商业控件ComponentOne for WPF要4w多呢) This proj ...

随机推荐

  1. nyoj 56 阶乘中素数的个数

    给定两个数m,n,其中m是一个素数. 将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m. 输入 第一行是一个整数s(0<s<=100),表示测试数据的组数随后的 ...

  2. ubuntu下git安装及连接github

    1.安装 sudo apt-get install git git-core git-gui git-doc git-svn git-cvs gitweb gitk git-email git-dae ...

  3. HW5.22

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  4. npm 国内镜像资源 --转载

    npm 是node.js 环境下的包管理器,非常强大智能. 生活这这片神奇的土地上,各种奇葩手段屡见不鲜啊. 为什么要换源? npm 官方站点 http://www.npmjs.org/ 并没有被墙, ...

  5. Yii防注入攻击笔记

    网站表单有注入漏洞须对所有用户输入的内容进行个过滤和检查,可以使用正则表达式或者直接输入字符判断,大部分是只允许输入字母和数字的,其它字符度不允许:对于内容复杂表单的内容,应该对html和script ...

  6. Unity定时器

    需求:制作定时器,运行3秒后执行第一次,之后每隔3秒执行一次操作. 1.使用变量在Update中计时 public class TestTimer : MonoBehaviour { private ...

  7. Dom4J对XML的创建、修改、删除等操作

    Dom4j也可以很方便完成XML文档的创建.元素的修改.文档的查询遍历等,但dom4j稍比jdom复杂一点,不过在大片文档的情况下dom4j的性能要不jdom好. # 准备 首先,提供相关的jar包 ...

  8. IOS开发之TableView替换默认的checkmark为自定义图像

    直接上代码: On cellForRowAtIndexPath: UIButton*button =[UIButton buttonWithType:UIButtonTypeCustom];CGRec ...

  9. Win7与虚拟机VMware下运行的Ubuntu共享文件夹

    安装VMware Tools,在VMware面板上选择“虚拟机-重新安装VMware tools…”,如下图所示: 在这里VMware虚拟了一个光盘镜像,我们需要把这个镜像挂载到本机的/mnt目录下面 ...

  10. [caffe]深度学习之图像分类模型VGG解读

    一.简单介绍 vgg和googlenet是2014年imagenet竞赛的双雄,这两类模型结构有一个共同特点是go deeper.跟googlenet不同的是.vgg继承了lenet以及alexnet ...