简体   繁体   English

使用水平StackPanel作为ItemsPanel滚动ItemsControl

[英]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) 我有一个ItemsControl ,我已经构建了我正在其中显示的项目( views:DisplayGroupView ),它们将水平扩展以显示所有内容而不是垂直(仅使用可用高度)

I've changed my ItemsPanel of the ItemsControl to use a StackPanel with Orientation="Horizontal" 我已经改变了我ItemsPanel中的ItemsControl使用StackPanelOrientation="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的XAML:

    <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: 我也尝试更改ItemsControls模板以包含一个scrollviewer,但这只是垂直堆叠的东西:

            <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; 如果你把它从ItemsControl拉出来并自己嵌入它,出于某种原因,这通常会起到解决方法的作用,如:

<ScrollViewer VerticalScrollBarVisibilty="Disabled" HorizontalScrollBarVisibility="Auto">
  <ItemsControl/>
</ScrollViewer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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