简体   繁体   English

暂时禁用 ScrollViewer 的滚动

[英]Disable the scroll of the ScrollViewer for a while

I have a ScrollViewer with an ItemPresenter inside.我有一个ScrollViewer ,里面有一个ItemPresenter The ItemsPresenter contains a few dropdowns, and when I open one of those, I'd like to disable the parent ScrollViewer 's scroll and only re-enable it only when the dropbox is closed. ItemsPresenter包含一些下拉菜单,当我打开其中一个下拉菜单时,我想禁用父级ScrollViewer的滚动,并且只有在下拉框关闭时才重新启用它。
By saying "disable" I mean prevent scrolling at all (even with the mouse wheel).通过说“禁用”,我的意思是完全阻止滚动(即使使用鼠标滚轮)。

I've tried to set the VerticalScrollBarVisibility to Disabled like this:我尝试将VerticalScrollBarVisibility设置为Disabled ,如下所示:

<ScrollViewer HorizontalScrollBarVisibility="Disabled"
              VerticalScrollBarVisibility="Disabled">
   <ItemsPresenter />
</ScrollViewer>

but that doesn't work either.但这也不起作用。
It just hides the scrollbar, but the mouse wheel still works .它只是隐藏了滚动条,但鼠标滚轮仍然有效

So, is there a way to completely disable the ScrollViewer 's scroll?那么,有没有办法完全禁用ScrollViewer的滚动?

Here is the full code that I have:这是我拥有的完整代码:

<ListView.Template>
   <ControlTemplate>
      <ScrollViewer HorizontalScrollBarVisibility="Disabled"
                    VerticalScrollBarVisibility="{Binding IsScrollEnabled, Converter={StaticResource BoolToVisibilityConverter}}">
         <ItemsPresenter />
      </ScrollViewer>
   </ControlTemplate>
</ListView.Template>

PS There are lots of some similar questions like this and this , but none of them is the one I wanted. PS有很多类似的问题this和this ,但没有一个是我想要的。

You can disable scrolling by handling the PreviewMouseWheel event for the ScrollViewer .您可以通过处理ScrollViewerPreviewMouseWheel事件来禁用滚动。

<ScrollViewer HorizontalScrollBarVisibility="Disabled"
              VerticalScrollBarVisibility="{Binding IsScrollEnabled, Converter={StaticResource BoolToVisibilityConverter}}"
              PreviewMouseWheel="UIElement_OnPreviewMouseWheel">
   <ItemsPresenter />
</ScrollViewer>
private void UIElement_OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
   e.Handled = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM