解决方法:
1、在窗体的load事件中添加:ListBox绑定鼠标滚轮事件MouseWheel事件
private void InitLoad()
{
lbSteplistBox.AddHandler(ListBox.MouseWheelEvent, new MouseWheelEventHandler(listBox_MouseWheel), true);
}
/// <summary>
/// 滚动条事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox_MouseWheel(object sender, MouseWheelEventArgs e)
{
ItemsControl items = (ItemsControl)sender;
ScrollViewer scroll = FindVisualChild<ScrollViewer>(items);
if (scroll != null)
{
int d = e.Delta;
if (d > 0)
{
scroll.LineRight();
}
if (d < 0)
{
scroll.LineLeft();
}
scroll.ScrollToVerticalOffset(scroll.VerticalOffset - e.Delta/100);
}
}
/// <summary>
/// 获得LisBox的ScrollViewer
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
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;
}