此文章意在解决在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. bzoj 1483 [HNOI2009]梦幻布丁(链表+启发式合并)

    1483: [HNOI2009]梦幻布丁 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1818  Solved: 761[Submit][Status ...

  2. HW4.45

    public class Solution { public static void main(String[] args) { int count = 0; for(int i = 1; i < ...

  3. hdoj 2568 前进

    前进 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  4. Robotium学习笔记二

    一. 控制测试用例的执行顺序 采用TestSuit方式来控制每条Case的运行顺序 Demo如下 public static Test suite() { TestSuite suite = new ...

  5. Sublime Text3使用及常用插件

    1.安装packages组件: 参考一: https://sublime.wbond.net/installation 参考二: http://blog.csdn.net/superskk6/arti ...

  6. java.lang.UnsupportedClassVersionError: Unsupported major.minor version 52.0的错误

    1.首先检查是不是jdk版本过低,如果过低的话就把jdk重新安装一下 2.在编译器的版本中设置一下,compiler中设置成与jdk版本相同

  7. ASP.NET用HttpListener实现文件断点续传

    本文转载:http://www.cnblogs.com/TianFang/archive/2007/01/03/610739.html 断点续传的原理很简单,就是在Http的请求和应答的报文头上和一般 ...

  8. C++ Virtual详解

    转自:http://www.cnblogs.com/xd502djj/archive/2010/09/22/1832912.html Virtual是C++ OO机制中很重要的一个关键字.只要是学过C ...

  9. centos6.5安装tomcat8.0.15

    首先需要在http://tomcat.apache.org/download-80.cgi下载最新安装包 安装tomcat 将apache-tomcat-8.0.15.tar.gz文件上传到/usr/ ...

  10. Xenomai 的模式切换浅析

    在Xenomai的用户空间下,有两种模式:primary mode (主模式) 和 secondary mode(次模式). 在主模式下调用Linux系统调用后程序就会进入次模式,反之,在次模式下调用 ...