none
how to enable horizontal scrolling with mouse wheel?

    Question

  • How do you enable WPF to respond to horizontal scrolling using the mouse tilt wheel? For example, I have a Microsoft Explorer mini mouse and have tried horizontally scrolling content contained within a ScrollViewer with 

    HorizontalScrollBarVisibility="Visible"
    

    but the content will not scroll horizontally. Vertical scrolling, however, works reliably as usual.

    Thanks!

    Wednesday, November 10, 2010 10:29 PM

Answers

  • Hi T.Webster,

    We could handle the PreviewMouseWheel event of the ScrollView (or other control), and find the ScrollViewer in the event handler. If the 'e.Delta' larger than zero, we could invoke the ScrollViewer. LineLeft method to scroll the HorizontalScrollBar.

     <Grid Background="Transparent">
      <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto" 
             PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
       <Rectangle Width="2000" Height="1000" Fill="Red"/>
      </ScrollViewer>
     </Grid>
    

    C#:

      private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
      {
       ScrollViewer scrollviewer= sender as ScrollViewer;
       if (e.Delta > 0)
        scrollviewer.LineLeft();
       else
        scrollviewer.LineRight();
       e.Handled = true;
      }
    
    Sincerely,
    Bob Bao

     

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Are you looking for a typical code sample? Please download all in one code framework !
    • Marked as answer by T. Webster Friday, November 12, 2010 3:36 AM
    Thursday, November 11, 2010 4:56 AM

All replies

  • Hi T.Webster,

    We could handle the PreviewMouseWheel event of the ScrollView (or other control), and find the ScrollViewer in the event handler. If the 'e.Delta' larger than zero, we could invoke the ScrollViewer. LineLeft method to scroll the HorizontalScrollBar.

     <Grid Background="Transparent">
      <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto" 
             PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
       <Rectangle Width="2000" Height="1000" Fill="Red"/>
      </ScrollViewer>
     </Grid>
    

    C#:

      private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
      {
       ScrollViewer scrollviewer= sender as ScrollViewer;
       if (e.Delta > 0)
        scrollviewer.LineLeft();
       else
        scrollviewer.LineRight();
       e.Handled = true;
      }
    
    Sincerely,
    Bob Bao

     

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Are you looking for a typical code sample? Please download all in one code framework !
    • Marked as answer by T. Webster Friday, November 12, 2010 3:36 AM
    Thursday, November 11, 2010 4:56 AM
  • Hello Bob,

    Thanks for that solution. I'm also looking for a way to scroll by listening to the mouse's middle tilt wheel.

    I think HwndSource.AddHook would work as a workaround in case this is not directly supported by WPF. 

    Thanks!

    • Edited by T. Webster Thursday, November 11, 2010 11:27 PM correction
    Thursday, November 11, 2010 6:45 PM
  • Hi T.Webster,

    Yes, if you have a special device to control the element, you could try to use Spy++ to get the Windows Message form the HwndWrapper, and add the hook to the WndProc method to catch the messages to control the WPF elements. 

    Sincerely,
    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Are you looking for a typical code sample? Please download all in one code framework !
    Friday, November 12, 2010 3:49 AM