此文章意在解决在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. 【JS】Beginner9:Arrays

    1.Lists of any kind of data 2.Index to retreve an element from the array 0 3.[] .length; .pop()/push ...

  2. HW2.6

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

  3. Codeforces149D - Coloring Brackets(区间DP)

    题目大意 要求你对一个合法的括号序列进行染色,并且需要满足以下条件 1.要么不染色,要么染红色或者蓝色 2.对于任何一对括号,他们当中有且仅有一个被染色 3.相邻的括号不能染相同的颜色 题解 用区间d ...

  4. 问题-delphi 调试(F8)错行处理

    在delphi 7中(其他版本也可能碰到该问题),编译后,代码左边显示蓝色小点,表示哪句代 码是被编译了的,可今天我的一些代码不能显示那些蓝点了,这样也就不能在上面设断点了,而且用F8跟踪发现执行的顺 ...

  5. java多态/重载方法——一个疑难代码引发的讨论

    直接上代码,看这个代码发现自己的基础有多差了.参考 http://www.cnblogs.com/lyp3314/archive/2013/01/26/2877205.html和http://hxra ...

  6. mysql-no-install 手动安装

    D:\mysql-5.5.28-win32 1.下载mysql-5.5.15-win32.zip,解压到本地D:\mysql. 2.将my-small.ini另存为my.ini .(根据情况可以选择别 ...

  7. eclipse调试的基本意义

    step into就是单步执行,遇到子函数就进入并且继续单步执行: step over是在单步执行时,在函数内遇到子函数时不会进入子函数内单步执行,而是将子函数整个执行完再停止,也就是把子函数整个作为 ...

  8. ip头、tcp头、udp头详解及定义,结合Wireshark抓包看实际情况

    公司的同事们在分析网页加载慢的问题,忽然使用到了Wireshark工具,我就像发现新大陆一样好奇,赶紧看了看,顺便复习了一下相关协议.上学时学的忘的差不多了,汗颜啊! 报文封装整体结构 mac帧头定义 ...

  9. 为虚机分配指定的IP

    在新建虚机的时候,在此处指定IP,则虚机创建完成后,会使用该IP(需要在地址池范围内)

  10. 远程使用Gpupdate(Hash,哈希)

    function Start-GPUpdate {     param     (         [String[]]         $ComputerName     )       $code ...