3

I have a ScrollViewer with an ItemPresenter inside. 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.
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:

<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?

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>

P.S. There are lots of some similar questions like this and this, but none of them is the one I wanted.

thatguy
  • 21,059
  • 6
  • 30
  • 40
Just Shadow
  • 10,860
  • 6
  • 57
  • 75
  • Your converter probably returns **Hidden** for the visibility, but that only hides the ScrollBar, but scrolling is still possible. It should return **Disabled** instead, which prevents scrolling. – mami Aug 06 '20 at 13:26
  • No, it was returning "Disabled". The strange thing there is that even if I hardcode "Disabled" right there (see the first sample), it still won't work. – Just Shadow Aug 06 '20 at 13:31

1 Answers1

3

You can disable scrolling by handling the PreviewMouseWheel event for the ScrollViewer.

<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;
}
thatguy
  • 21,059
  • 6
  • 30
  • 40