Uwp使用WrapGrid分组GridView

编程入门 行业动态 更新时间:2024-10-23 06:27:56
本文介绍了Uwp使用WrapGrid分组GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个UWP应用程序,我试图在每个gridview组中对齐我的项目。

我有这个:

第1组 数据1 |数据2 |数据3 数据1 |数据2 |数据3 数据1 |数据2 |数据3 组2 数据1 |数据2 |数据3 数据1 |数据2 |数据3

我想要:

第1组 ______________________________数据1 |数据2 |数据3 ______________________________数据1 |数据2 |数据3 ______________________________数据1 |数据2 |数据3 第2组 ______________________________数据1 |数据2 |数据3 ______________________________数据1 |数据2 |数据3

我为了对齐我的项目我使用WrapGrid。 我也试过用非分组的gridview,我的代码工作正常。

< GridView Grid.Column = 0ItemsSource ={Binding Source = {StaticResource GroupBycodeFamille}}ItemTemplate ={StaticResource DataTempateCatalogue}> < GridView.GroupStyle> < GroupStyle> < GroupStyle.HeaderTemplate> < DataTemplate x:DataType =viewModels:GroupInfoList> < TextBlock Text ={x:Bind Key}Style ={ThemeResource TitleTextBlockStyle}/> < / DataTemplate> 您能帮我吗?

提前致谢 解决方案

您可以通过设置

I'm developing an UWP app and I'm trying to align horizontaly my items in each gridview groups

I have this :

GROUP 1 Data 1 | Data 2 | Data 3 Data 1 | Data 2 | Data 3 Data 1 | Data 2 | Data 3 GROUP 2 Data 1 | Data 2 | Data 3 Data 1 | Data 2 | Data 3

and I want this :

GROUP 1 ______________________________Data 1 | Data 2 | Data 3 ______________________________Data 1 | Data 2 | Data 3 ______________________________Data 1 | Data 2 | Data 3 GROUP 2 ______________________________Data 1 | Data 2 | Data 3 ______________________________Data 1 | Data 2 | Data 3

I order to align my items I use a WrapGrid. I also tried with a non grouped gridview and my code works fine.

<GridView Grid.Column="0" ItemsSource="{Binding Source={StaticResource GroupBycodeFamille}}" ItemTemplate="{StaticResource DataTempateCatalogue}"> <GridView.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate x:DataType="viewModels:GroupInfoList"> <TextBlock Text="{x:Bind Key}" Style="{ThemeResource TitleTextBlockStyle}"/> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle> </GridView.GroupStyle> <GridView.ItemsPanel> <ItemsPanelTemplate> <WrapGrid Orientation="Horizontal" HorizontalAlignment="Center"/> </ItemsPanelTemplate> </GridView.ItemsPanel> </GridView>

Could you please help me ?

Thanks in advance

解决方案

You can get what you want by setting GroupStyle.Panel property. This property sets a template that creates the panel used to lay out the items. But GridViewItem has two templates, when the GridView's ItemsPanel is an ItemsWrapGrid (the default) or ItemsStackPanel, GridViewItem uses the template which uses a GridViewItemPresenter instead of a UIElement tree to improve grid performance. And while using this template, setting GroupStyle.Panel property won't affect the GridView. We need GridViewItem use its second template by setting GridView's ItemsPanel to some other Panel. As your group is aligned vertically, we can set GridView's ItemsPanel to:

<GridView.ItemsPanel> <ItemsPanelTemplate> <StackPanel HorizontalAlignment="Center" Orientation="Vertical" /> </ItemsPanelTemplate> </GridView.ItemsPanel>

Then we can set GroupStyle.Panel with a left Margin to achieve your goal. But please note that:

The root element of the template declared for the ItemsPanelTemplate in the GroupStyle.Panel property cannot be a virtualizing panel. Virtualizing panels are defined as any type that derives from VirtualizingPanel, for example the VirtualizingStackPanel class.

So we can't use WrapGrid here, we can use a StackPanel but the items will be arranged into a single line. To arrange items into multi lines, we need a Panel like WrapPanel in WPF. We can customize it or use a third party WrapPanel like what in WinRTXamlToolkit. Using WinRTXamlToolkit for example:

xmlns:toolkit="using:WinRTXamlToolkit.Controls" <GroupStyle.Panel> <ItemsPanelTemplate> <toolkit:WrapPanel Margin="200,0,0,0" Orientation="Horizontal" /> </ItemsPanelTemplate> </GroupStyle.Panel>

The complete XAML code may like:

<GridView Grid.Column="0" ItemsSource="{Binding Source={StaticResource GroupBycodeFamille}}" ItemTemplate="{StaticResource DataTempateCatalogue}"> <GridView.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate x:DataType="viewModels:GroupInfoList"> <TextBlock Text="{x:Bind Key}" Style="{ThemeResource TitleTextBlockStyle}"/> </DataTemplate> </GroupStyle.HeaderTemplate> <GroupStyle.Panel> <ItemsPanelTemplate> <toolkit:WrapPanel Margin="200,0,0,0" Orientation="Horizontal" /> </ItemsPanelTemplate> </GroupStyle.Panel> </GroupStyle> </GridView.GroupStyle> <GridView.ItemsPanel> <ItemsPanelTemplate> <StackPanel HorizontalAlignment="Center" Orientation="Vertical" /> </ItemsPanelTemplate> </GridView.ItemsPanel> </GridView>

Following is the output when I tested in my side:

更多推荐

Uwp使用WrapGrid分组GridView

本文发布于:2023-11-10 15:58:53,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Uwp   WrapGrid   GridView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!