Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML Horizontal Stackpanel Scrolling

I am having problems getting horizontal stackpanels to scroll from within a scrollviewer. What I'm trying to do is more complex than my example, but after removing variables I think I can figure everything out if I can solve this last problem.

Basically, I can't get a scrollviewer to scroll horizontally when it contains a horizontal stackpanel.

Here is the sample XAML:

   <ScrollViewer>
        <StackPanel Orientation="Horizontal">
            <Image Source="test.png" Width="400" Height="400"/>
            <Image Source="test.png" Width="400" Height="400"/>
            <Image Source="test.png" Width="400" Height="400"/>
            <Image Source="test.png" Width="400" Height="400"/>
            <Image Source="test.png" Width="400" Height="400"/>
            <Image Source="test.png" Width="400" Height="400"/>
            <Image Source="test.png" Width="400" Height="400"/>
        </StackPanel>
    </ScrollViewer>

Strangely, if I just swap the orientation from Horizontal to Vertical, it scrolls just fine. I've read on multiple posts that stackpanels have issues that can make them a poor fit for scrollviewers, so I tried it with a grid as well, but get the same results.

  <ScrollViewer>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="400"/>
                <ColumnDefinition Width="400"/>
                <ColumnDefinition Width="400"/>
                <ColumnDefinition Width="400"/>
                <ColumnDefinition Width="400"/>
                <ColumnDefinition Width="400"/>
            </Grid.ColumnDefinitions>                
            <Image Source="test.png" Width="400" Height="400"  Grid.Column="0"/>
            <Image Source="test.png" Width="400" Height="400"  Grid.Column="1"/>
            <Image Source="test.png" Width="400" Height="400"  Grid.Column="2"/>
            <Image Source="test.png" Width="400" Height="400"  Grid.Column="3"/>
            <Image Source="test.png" Width="400" Height="400"  Grid.Column="4"/>
            <Image Source="test.png" Width="400" Height="400"  Grid.Column="5"/>
        </Grid>
    </ScrollViewer>

It seems so simple that I feel like I'm misunderstanding something fundamental. If anyone can help out, I'd be extremely grateful.

like image 890
Riot9 Avatar asked Oct 10 '13 06:10

Riot9


People also ask

How do I make my StackPanel scrollable?

Put ScrollViewer inside StackPanel . You could use a ListBox instead. It will scroll automatically.

How do I add a scrollbar to StackPanel?

Setting this property has no effect. If you require physical scrolling instead of logical scrolling, wrap the StackPanel in a ScrollViewer and set its CanContentScroll property to false."

How do I add a vertical scrollbar in WPF?

In order to enable horizontal and/or vertical scrollbars you need to set the ScrollViewer's attached properties HorizontalScrollBarVisibility and/or VerticalScrollBarVisibility.


1 Answers

You just have to turn on horizontal scrolling. It's hidden by default (but the vertical one is not, hence the confusion).

<ScrollViewer HorizontalScrollBarVisibility="Auto">
like image 154
McGarnagle Avatar answered Nov 12 '22 08:11

McGarnagle