简体   繁体   中英

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM