Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable the scroll of the ScrollViewer for a while

Tags:

c#

.net

mvvm

wpf

xaml

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.

like image 511
Just Shadow Avatar asked Sep 11 '25 03:09

Just Shadow


1 Answers

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;
}
like image 141
thatguy Avatar answered Sep 12 '25 18:09

thatguy