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);