locked
Horizontal Scrolling RRS feed

  • Question

  • I have a control who's contents (a grid) are a bit too big, so I put it inside a scrollviewer. Now it has a vertical and horizontal scrollbar.

     

    The vertical scrollbar works like it should, when scroling with the mousewheel or scrolling using the mousepad it scrolls. But I would like to add the possibility to scroll horizontally as well.

     

    How can this be done?

    Friday, November 18, 2011 7:29 PM

Answers

  • Following code works for me for horizontal scrolling

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
        <Grid Background="Transparent">
            <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto" 
             PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
                <Rectangle Width="2000" Height="1000" Fill="Red"/>
            </ScrollViewer>
        </Grid>
    </Window>

    Code Behind

        Private Sub ScrollViewer_PreviewMouseWheel(sender As System.Object, e As System.Windows.Input.MouseWheelEventArgs)
            Dim viewer As ScrollViewer = sender
            If e.Delta > 0 Then
                viewer.LineLeft()
            Else
                viewer.LineRight()
            End If
     
            e.Handled = True
        End Sub

    You can also try code from following link

    http://stackoverflow.com/questions/5426232/how-can-i-make-wpf-scrollviewer-middle-click-scroll


    Gaurav Khanna | Microsoft VB.NET MVP
    • Proposed as answer by Sheldon _Xiao Tuesday, November 22, 2011 7:23 AM
    • Marked as answer by Sheldon _Xiao Monday, November 28, 2011 11:03 AM
    Sunday, November 20, 2011 7:34 AM

All replies

  • It can be done like this.

    You can set the setting for the Vertical and Horizontal scrollbars visibility, to Visible or Auto, or whatever your desire is.

    <Grid x:Name="LayoutRoot">
            <ScrollViewer Height="250" Width="250" ScrollViewer.HorizontalScrollBarVisibility="Visible">
                <Grid Height="500" Width="500"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Center">            
                    
                </Grid>
            </ScrollViewer>
        </Grid>
    



    Developing is part of being a developer.
    Friday, November 18, 2011 9:24 PM
  • Making them visible was not a problem. I can see the horizontal scrollbar (and the vertical), I can scroll by clicking the arrows on the left and right of the scrollbar and by dragging it. that's not the problem.

     

    The vertical one scrolls by rotating the mousewheel or by moving two fingers across the mousepad vertically. What I want is that when I move horizontally across the mousepad (or tilt with the mousewheel) that the horizontal one responds.

    Friday, November 18, 2011 10:23 PM
  • Saturday, November 19, 2011 6:40 PM
  • I tried the code but it didn't work properly. Now if I scroll normally, it scrolls horizontally and scrolling horizontally or tilting the mousewheel still doesn't do anything.

     

    Maybe someone else should try it? It could be my computer/laptop.

    Saturday, November 19, 2011 7:20 PM
  • Following code works for me for horizontal scrolling

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
        <Grid Background="Transparent">
            <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto" 
             PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
                <Rectangle Width="2000" Height="1000" Fill="Red"/>
            </ScrollViewer>
        </Grid>
    </Window>

    Code Behind

        Private Sub ScrollViewer_PreviewMouseWheel(sender As System.Object, e As System.Windows.Input.MouseWheelEventArgs)
            Dim viewer As ScrollViewer = sender
            If e.Delta > 0 Then
                viewer.LineLeft()
            Else
                viewer.LineRight()
            End If
     
            e.Handled = True
        End Sub

    You can also try code from following link

    http://stackoverflow.com/questions/5426232/how-can-i-make-wpf-scrollviewer-middle-click-scroll


    Gaurav Khanna | Microsoft VB.NET MVP
    • Proposed as answer by Sheldon _Xiao Tuesday, November 22, 2011 7:23 AM
    • Marked as answer by Sheldon _Xiao Monday, November 28, 2011 11:03 AM
    Sunday, November 20, 2011 7:34 AM
  • Hi roy19,

    how about your concern?

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, November 22, 2011 7:23 AM