ScrollViewer mouse wheel not scrolling
Asked Answered
G

6

75

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.

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.

Fourth attempt (constant size of the ScrollViewer)

<ScrollViewer Height="450" Width="200">
    <ListView ItemsSource="{Binding FileViewModels}"
              PreviewMouseDown="ListView_MouseMove"/>
</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?

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?

Galactic answered 26/4, 2013 at 10:43 Comment(0)
C
127

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;
 }
Commonly answered 26/4, 2013 at 11:52 Comment(2)
Nice, this works - thank you. But I am still not sure, whether having a ListView inside a ScrollViewer is good style (see edit section above).Galactic
I found dividing e.Delta by 10 made a finer granularity to the scrolling (less chunky). (Good answer, though!)Subsidy
I
28

This would probably be the most comfortable solution:

<ListView.Template>
    <ControlTemplate>
        <ScrollViewer>
            <ItemsPresenter></ItemsPresenter> 
        </ScrollViewer>
    </ControlTemplate>
</ListView.Template>
Incubate answered 3/6, 2014 at 23:29 Comment(6)
Best Solution. Might be possible to aditionally put Itemspresenter into a Grid or DockPanelSupport
I am facing a small issue with your solution.. The header is now vanished.. Any suggestions?Hohenlinden
@ShalvinAbraham what header? Listview has no header i'm aware of?Incubate
I am new to WPF, so maybe my misunderstanding.. I am using ListView as : <ListView.View> <GridView> <GridViewColumn> with headers.Hohenlinden
I was able to fix the issue by adding <ListView MaxHeight="200" ScrollViewer.CanContentScroll="True" to the list view element. Now it supports mouse wheel, as well as headersHohenlinden
This is an interesting idea, but you don't get a scrollbar, and the mousewheel still doesn't work.Zwolle
N
26

For me this worked:

<ListView.Template>
    <ControlTemplate>
        <!-- Empty template to allow ScrollViewer to capture mouse scroll -->
        <ItemsPresenter />
    </ControlTemplate>
</ListView.Template>

instead of this:

<ListView.Template>
    <ControlTemplate>
        <ScrollViewer>
            <ItemsPresenter></ItemsPresenter>
        </ScrollViewer>
    </ControlTemplate>
</ListView.Template>
Newhall answered 6/6, 2018 at 8:24 Comment(0)
G
8

In my case this helped:

<ScrollViewer ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Auto" >
    <DataGrid x:Name="dataGrid" SelectionMode="Single" ItemsSource="{Binding}"  SelectedValuePath="{Binding Item}" AutoGenerateColumns="True">
    </DataGrid>
</ScrollViewer>

The design was disabling VerticalScrollBarVisibility attribute in outer scope , i.e. in ScrollViewer.

Gregoire answered 13/10, 2016 at 7:35 Comment(2)
This should be the accepted answer. It's working & simple. Thank you!Estes
Agree. This is the only answer which works for me! Thanks!Tremblay
L
7
<ScrollViewer Background="Transparent">

If Background is null, the mouse wheel will not work on ScrollViewer. You can set the Background to Transparent or some other value.

Leonilaleonine answered 4/7, 2015 at 4:55 Comment(1)
this works for me only, if the mouse pointer is over the scrollbar, but not if it is over the contentSame
R
1

I want to add some comment to the solution Rocky provided. It worked fine for me, but later I needed to use it in a different window to scroll Grid. I faced a problem: the ScrollViewer did not scroll to the bottom end. The reason was because of attempts to set the invalid VerticalOffset value. The code below works fine for me (just need to change PreviewMouseWheel handler:

private void UIElement_OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    ScrollViewer scroll = (ScrollViewer)sender;
    if (e.Delta < 0)
    {
        if (scroll.VerticalOffset - e.Delta <= scroll.ExtentHeight - scroll.ViewportHeight)
        {
            scroll.ScrollToVerticalOffset(scroll.VerticalOffset - e.Delta);
        }
        else
        {
            scroll.ScrollToBottom();
        }
    }
    else
    {
        if (scroll.VerticalOffset + e.Delta > 0)
        {
            scroll.ScrollToVerticalOffset(scroll.VerticalOffset - e.Delta);
        }
        else
        {
            scroll.ScrollToTop();
        }
    }
    e.Handled = true;
}
Ricci answered 24/6, 2019 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.