Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling in a ItemsControl while using a horizontal StackPanel as the ItemsPanel

I have an ItemsControl, I've built the items that I'm displaying in it (views:DisplayGroupView) in such a way that they will expand horizontally to show all their contents and not vertically (only using the available height)

I've changed my ItemsPanel of the ItemsControl to use a StackPanel with Orientation="Horizontal"

Layout wise it's perfect, but no matter what I do I can't get it to scroll horizontally so I can see everything.

This is the XAML for the ItemsControl:

    <ItemsControl ItemsSource="{Binding DisplayGroups}" Grid.Row="1" Margin="120,20,120,20" VerticalContentAlignment="Stretch">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate >
                <StackPanel Orientation="Horizontal" ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.HorizontalScrollBarVisibility="Visible"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <views:DisplayGroupView Margin="0,0,20,0" DataContext="{Binding}" VerticalAlignment="Stretch"></views:DisplayGroupView>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

This lays everything out okay, but won't scroll. I've also tried changing the ItemsControls template to include a scrollviewer, but this only stacks things vertically:

            <ItemsControl.Template>
                <ControlTemplate>
                    <ScrollViewer x:Name="ScrollViewer" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch" ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.VerticalScrollMode="Disabled">
                        <ItemsPresenter VerticalAlignment="Stretch"/>
                    </ScrollViewer>
                </ControlTemplate>
            </ItemsControl.Template>

How can I get the horizontal layout while still being able to scroll?

like image 319
PhonicUK Avatar asked Aug 07 '13 22:08

PhonicUK


1 Answers

If you pull it out of the ItemsControl and embed it by itself, for some reason that often acts as a workaround so something like;

<ScrollViewer VerticalScrollBarVisibilty="Disabled" HorizontalScrollBarVisibility="Auto">
  <ItemsControl/>
</ScrollViewer>
like image 66
Chris W. Avatar answered Oct 25 '22 13:10

Chris W.