的ScrollViewer鼠标滚轮不工作鼠标、滚轮、工作、ScrollViewer

2023-09-04 01:39:21 作者:将爱意邮寄

我目前的工作对我的第一个WPF项目,并努力使列表视图滚动。 起初我还以为这可以通过简单地限制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)

Second attempt (using 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的大小)

Fourth attempt (constant size of the 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?

修改 也许我应该回到我的第一个解决方案。      显然,ListView的模板已包含的ScrollViewer。那么剩下的问题是,我不能选择,因为处理previewMouseDown事件的项目后,拖动滚动条(通过鼠标滚轮滚动在这种情况下仍然有效)。我应该处理的项目拖动不同的(它的工作对我很好,想增加一个滚动条之前)?还是有一种方法来检测,如果光标滚动条以上(这样我就可以再取消它能使滚动项目)? 还是有什么其他的建议?

Edit Maybe I should go back to my first solution. 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?

推荐答案

这可能会帮助你。

private void ListViewScrollViewer_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
   ScrollViewer scv = (ScrollViewer)sender;
   scv.ScrollToVerticalOffset(scv.VerticalOffset - e.Delta);
   e.Handled = true;
 }