CR的代码文本

all for learning about the world
  订阅 订阅  :: 管理

WPF ListBox响应鼠标滚轮

Posted on 2013-11-19 16:26  mumuliang  阅读(1903)  评论(0编辑  收藏  举报
public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
        {
            if (obj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is T)
                    {
                        return (T)child;
                    }
                    T childItem = FindVisualChild<T>(child);
                    if (childItem != null) return childItem;
                }
            }
            return null;
        }
        void lbMatFile_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            ItemsControl items = (ItemsControl)sender;
            ScrollViewer scroll = FindVisualChild<ScrollViewer>(items);
            if (scroll != null)
            {
                scroll.ScrollToHorizontalOffset(scroll.HorizontalOffset - e.Delta);
            }
        }

 

要这样添加事件处理

lbMatFile.AddHandler(System.Windows.Controls.ListBox.MouseWheelEvent, new MouseWheelEventHandler(lbMatFile_MouseWheel), true);