限制 GridView C# UWP 中的项目数
Limiting number of items in a GridView C# UWP
我使用绑定到集合的网格视图。该集合包含未知数量的项目,我想限制 GridView 在一行中仅显示 5 个项目。每个项目都保存在一个包含文本块和图像的堆栈面板中。
一个。如何限制 GridView 的项目数?
B. 如果我不想限制它,我怎样才能制作一个 1 行的 gridview,用一个小箭头将网格滚动到一边?
这是我的 xaml 代码:
<GridView Name="ListViewGrid" Background="Azure"
Grid.Row="2"
ItemsSource="{x:Bind ForeCasts}"
Foreground="Chartreuse"
HorizontalAlignment="Stretch" >
<GridView.ItemTemplate>
<DataTemplate x:DataType="data:ForeCast">
<StackPanel Orientation="Vertical" Margin="20,20,20,20" Height="260" Width="260">
<TextBlock HorizontalAlignment="Center" Margin="10,10,10,10">
<Run Text="{x:Bind TempString}" FontSize="24" Foreground="Black"/>
<Run Text="°" FontFamily="Segoe Print" FontSize="24"/>
<Run Text="C" FontSize="24"/>
</TextBlock>
<Image Source="{x:Bind ImageString}" Width="60" Height="60"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
The collection is holding an unknown number of items and I want to limit the GridView to show only 5 items in one row.
GridView uses ItemsWrapGrid as the default ItemsPanel. And ItemsWrapGrid has a MaximumRowsOrColumns property. With this property we can limit the maximum rows or columns that will present before wrapping. This property works with Orientation属性。要在一行中只显示 5 个项目,我们可以这样设置:
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="5" Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
请注意,MaximumRowsOrColumns
仅限制最大数量,如果 GridView
不够大,您可能会看到较少的项目。
If I don't want to limit it how can I make a gridview of 1 row which with a little arrow scroll the grid to the side?
要显示水平堆叠的集合,我们通常使用 ListView. If you do want to use GridView
, you can using ItemsStackPanel 而不是 ItemsWrapGrid
,如下所示:
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
要启用滚动,您可能还需要设置 ScrollViewer.HorizontalScrollBarVisibility property and ScrollViewer.HorizontalScrollMode 属性,如下所示:
<GridView Name="ListViewGrid"
Grid.Row="2"
HorizontalAlignment="Stretch"
Background="Azure"
Foreground="Chartreuse"
ItemsSource="{x:Bind ForeCasts}"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.HorizontalScrollMode="Enabled">
<GridView.ItemTemplate>
<DataTemplate x:DataType="data:ForeCast">
<StackPanel Width="260"
Height="260"
Margin="20,20,20,20"
Orientation="Vertical">
<TextBlock Margin="10,10,10,10" HorizontalAlignment="Center">
<Run FontSize="24" Foreground="Black" Text="{x:Bind TempString}" />
<Run FontFamily="Segoe Print" FontSize="24" Text="°" />
<Run FontSize="24" Text="C" />
</TextBlock>
<Image Width="60" Height="60" Source="{x:Bind ImageString}" />
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
我使用绑定到集合的网格视图。该集合包含未知数量的项目,我想限制 GridView 在一行中仅显示 5 个项目。每个项目都保存在一个包含文本块和图像的堆栈面板中。
一个。如何限制 GridView 的项目数? B. 如果我不想限制它,我怎样才能制作一个 1 行的 gridview,用一个小箭头将网格滚动到一边?
这是我的 xaml 代码:
<GridView Name="ListViewGrid" Background="Azure"
Grid.Row="2"
ItemsSource="{x:Bind ForeCasts}"
Foreground="Chartreuse"
HorizontalAlignment="Stretch" >
<GridView.ItemTemplate>
<DataTemplate x:DataType="data:ForeCast">
<StackPanel Orientation="Vertical" Margin="20,20,20,20" Height="260" Width="260">
<TextBlock HorizontalAlignment="Center" Margin="10,10,10,10">
<Run Text="{x:Bind TempString}" FontSize="24" Foreground="Black"/>
<Run Text="°" FontFamily="Segoe Print" FontSize="24"/>
<Run Text="C" FontSize="24"/>
</TextBlock>
<Image Source="{x:Bind ImageString}" Width="60" Height="60"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
The collection is holding an unknown number of items and I want to limit the GridView to show only 5 items in one row.
GridView uses ItemsWrapGrid as the default ItemsPanel. And ItemsWrapGrid has a MaximumRowsOrColumns property. With this property we can limit the maximum rows or columns that will present before wrapping. This property works with Orientation属性。要在一行中只显示 5 个项目,我们可以这样设置:
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="5" Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
请注意,MaximumRowsOrColumns
仅限制最大数量,如果 GridView
不够大,您可能会看到较少的项目。
If I don't want to limit it how can I make a gridview of 1 row which with a little arrow scroll the grid to the side?
要显示水平堆叠的集合,我们通常使用 ListView. If you do want to use GridView
, you can using ItemsStackPanel 而不是 ItemsWrapGrid
,如下所示:
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
要启用滚动,您可能还需要设置 ScrollViewer.HorizontalScrollBarVisibility property and ScrollViewer.HorizontalScrollMode 属性,如下所示:
<GridView Name="ListViewGrid"
Grid.Row="2"
HorizontalAlignment="Stretch"
Background="Azure"
Foreground="Chartreuse"
ItemsSource="{x:Bind ForeCasts}"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.HorizontalScrollMode="Enabled">
<GridView.ItemTemplate>
<DataTemplate x:DataType="data:ForeCast">
<StackPanel Width="260"
Height="260"
Margin="20,20,20,20"
Orientation="Vertical">
<TextBlock Margin="10,10,10,10" HorizontalAlignment="Center">
<Run FontSize="24" Foreground="Black" Text="{x:Bind TempString}" />
<Run FontFamily="Segoe Print" FontSize="24" Text="°" />
<Run FontSize="24" Text="C" />
</TextBlock>
<Image Width="60" Height="60" Source="{x:Bind ImageString}" />
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>