ScrollViewer鼠标滚轮不滚动

ScrollViewer鼠标滚轮不滚动

本文介绍了ScrollViewer鼠标滚轮不滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在第一个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?

修改也许我应该回到我的第一个解决方案.

EditMaybe 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鼠标滚轮不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 06:34