问题描述
我使用 GridView 在 WinRT XAML 应用程序中显示不同组的项目.一切都很好,除了 ItemsPanelTemplate 使用了一个环绕网格,当我的项目空间不足时,它会垂直堆叠我的项目.
I am using GridView for displaying groups of different sets of items in a WinRT XAML app. Everything works well, except that the ItemsPanelTemplate uses a wrapping grid which stacks my items vertically when it's out of space.
所以,我尝试使用 StackPanel,如下所示:
So, I tried to use StackPanel, like this:
<GroupStyle.Panel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" Visibility="Visible" />
</ItemsPanelTemplate>
</GroupStyle.Panel>
这些项目是垂直堆叠的,这很好,但现在的问题是我无法滚动它们,而且它们不适合屏幕.所以我尝试启用垂直滚动:
The items are stacked vertically, and that's great, but the problem is now that I can't scroll them, and they don't fit on the screen. So I tried enabling vertical scrolling:
<GroupStyle.Panel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" Visibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollMode="Enabled"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
但这行不通.有什么建议如何在 GridView 组内完成垂直滚动?
But that doesn't work.Any suggestions how to accomplish vertical scrolling inside GridView group?
编辑 1:
我也试过这个:
<GroupStyle.Panel>
<ItemsPanelTemplate>
<ScrollViewer VerticalScrollBarVisibility="Visible"
HorizontalScrollMode="Disabled"
ZoomMode="Disabled"
VerticalScrollMode="Enabled">
<StackPanel Orientation="Vertical" Visibility="Visible" />
</ScrollViewer>
</ItemsPanelTemplate>
</GroupStyle.Panel>
这会破坏调试器,因为 ItemsPanelTemplate 需要一个面板作为子项.
This breaks the debugger as the ItemsPanelTemplate needs a panel as a child.
推荐答案
好的,我终于解决了!可能涉及的对象:
OK, I finally solved it! To whom it may concern:
<GroupStyle.ContainerStyle>
<Style TargetType="GroupItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GroupItem">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentPresenter Content="{TemplateBinding Content}" Grid.Row="0"/>
<ItemsControl x:Name="ItemsControl2" ItemsSource="{Binding GroupItems}" Grid.Row="1">
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer x:Name="ScrollViewer" VerticalScrollBarVisibility="Hidden" VerticalScrollMode="Enabled" HorizontalScrollBarVisibility="Disabled" HorizontalScrollMode="Disabled">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
使用网格来确保 ScrollViewer 正确缩放很重要.
It's important that you use the Grid to make sure that the ScrollViewer scales correctly.
这篇关于WinRT XAML 中 GridView 项目组内的垂直滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!