Activate horizontal scrolling with mouse on ListView

后端 未结 4 1130
不思量自难忘°
不思量自难忘° 2020-12-18 13:30

I\'ve got a custom horizontal ListView with custom ScrollViewer inside it\'s template (created with Blend). I want it to scroll horizontally when using mouse scrolling wheel

4条回答
  •  太阳男子
    2020-12-18 14:08

    if you implement IScrollInfo you can override the MouseWheelUp to do MouseWheelLeft and down\right the in same way

    edit (much more simple):

    add to your ScrollViewer PreviewMouseWheel

    private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
            {
                if (e.Delta < 0) // wheel down
                {
                    if (myScrollViewer.HorizontalOffset + e.Delta > 0)
                    {
                        myScrollViewer.ScrollToHorizontalOffset(myScrollViewer.HorizontalOffset + e.Delta);  
                    }
                    else
                    {
                        myScrollViewer.ScrollToLeftEnd();
                    }
                }
                else //wheel up
                {
                    if (myScrollViewer.ExtentWidth > myScrollViewer.HorizontalOffset + e.Delta)
                    {
                        myScrollViewer.ScrollToHorizontalOffset(myScrollViewer.HorizontalOffset + e.Delta);  
                    }
                    else
                    {
                        myScrollViewer.ScrollToRightEnd();
                    }
                }
    
            }
    

    xaml:

     
    

提交回复
热议问题