ScrollViewer 鼠标滚轮不滚动 [英] ScrollViewer mouse wheel not scrolling
问题描述
我目前正在处理我的第一个 WPF 项目并尝试使 ListView
可滚动.起初我认为这可以通过简单地限制 ListView
的宽度和高度来轻松完成,从而在内容超出其空间时强制滚动条自动出现.起初这似乎很好,但由于处理了 PreviewMouseDown
事件(允许拖动列表的项目),它在选择项目后不起作用.
I am currently working on my first WPF project and trying to make a ListView
scrollable.
At first I thought this could be easily done by simply limiting the ListView
's width and height and thus forcing a scrollbar to appear automatically whenever the content exceeds its space. This seemed fine at first but due to the handled PreviewMouseDown
event (which enables dragging the list's items) it doesn't work after selecting an item.
第二次尝试(使用ScrollViewer
)
<ScrollViewer>
<ListView ItemsSource="{Binding FileViewModels}"
PreviewMouseDown="ListView_MouseMove"
Height="450" Width="200"/>
</ScrollViewer>
当然,每当列表的内容变得大于其最大高度时,这会导致第二个滚动条.选择一个项目后拖动栏仍然不起作用.
Of course, this resulted in a second scrollbar whenever the list's content became larger than its max height. And dragging the bar still didn't work after selecting an item.
第三(相当愚蠢)尝试(禁用滚动条重复)
Third (quite foolish) attempt (disabling scrollbar duplicate)
<ScrollViewer>
<ListView ItemsSource="{Binding FileViewModels}"
PreviewMouseDown="ListView_MouseMove"
Height="450" Width="200"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"/>
</ScrollViewer>
这删除了重复的滚动条并通过鼠标滚轮启用滚动但禁用了滚动条,因此您无法通过单击和拖动来移动它.
This removed the scrollbar duplicate and enabled scrolling via mouse wheel but disabled the scrollbar, so you couldn't move by clicking and dragging it.
第四次尝试(ScrollViewer
的恒定大小)
<ScrollViewer Height="450" Width="200">
<ListView ItemsSource="{Binding FileViewModels}"
PreviewMouseDown="ListView_MouseMove"/>
</ScrollViewer>
从 ListView
中移除宽度/高度约束并将其移至 ScrollViewer
.这将启用滚动条并删除重复项.不幸的是,鼠标滚轮不再工作(拖动滚动条工作正常).
Removed the width/height constraint from the ListView
and moved it to the ScrollViewer
. This enables the scrollbar and removes the duplicate. Unfortunately the mouse wheel doesn't work anymore (dragging the scroll bar works fine).
有人可以向我解释为什么鼠标滚轮不再起作用以及如何解决这个问题吗?
Could somebody please explain to me why the mouse wheel doesn't work anymore and how to fix this?
编辑也许我应该回到我的第一个解决方案.
Edit Maybe I should go back to my first solution.
显然,ListView
的模板已经包含一个 ScrollViewer
.剩下的问题是我无法在选择项目后拖动滚动条,因为处理了 PreviewMouseDown
事件(在这种情况下,通过 MouseWheel 滚动仍然有效).我是否应该以不同的方式处理项目的拖动(在想要添加滚动条之前,它对我来说很好用)?或者有没有办法检测光标是否在滚动条上方(这样我就可以取消选择启用滚动的项目)?或者有什么其他的建议?
Obviously, the ListView
's template already contains a ScrollViewer
. The remaining problem would then be that I cannot drag the scrollbar after selecting an item because of the handled PreviewMouseDown
event (scrolling via MouseWheel still works in that case). Should I handle the dragging of items differently (it worked fine for me, before wanting to add a scrollbar)? Or is there a way to detect if the cursor is above the scrollbar (so I could then deselect the item which enables scrolling)?
Or are there any other suggestions?
推荐答案
这可能对您有所帮助..
This may help you..
private void ListViewScrollViewer_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
ScrollViewer scv = (ScrollViewer)sender;
scv.ScrollToVerticalOffset(scv.VerticalOffset - e.Delta);
e.Handled = true;
}
这篇关于ScrollViewer 鼠标滚轮不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!