Wpf ScrollViewer with WrapPanel 使用鼠标滚轮水平滚动内容
为WrapPanel添加水平滚动条,当禁用垂直滚动条后使用鼠标无法滚动,竟然还需要自己写代码来实现,真真是挺无语呢,不知道算不算是一个bug。
<Grid Background="Transparent">
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto"
PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
<Rectangle Width="2000" Height="1000" Fill="Red"/>
</ScrollViewer>
</Grid>
private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
ScrollViewer scrollviewer= sender as ScrollViewer;
if (e.Delta > 0)
scrollviewer.LineLeft();
else
scrollviewer.LineRight();
e.Handled = true;
}
可以实现效果,感觉滚动不是很灵活,自作聪明的调用了两次LineLeft()
,搞定。
private void scrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
ScrollViewer sv = sender as ScrollViewer;
//move twice make it flexible
if (e.Delta>0)
{
sv.LineLeft();
sv.LineLeft();
}
else
{
sv.LineRight();
sv.LineRight();
}
e.Handled = true;
}
原文链接 how to enable horizontal scrolling with mouse wheel?
附赠一个WinRT版的解决方案
ScrollViewer Horizontal mouse wheel scrolling
From what I've done so far you should just be able to set the ScollViewer's style to
Style="{StaticResource HorizontalScrollViewerStyle}"
Which is defined in StandardStyles.xaml