gpt4 book ai didi

gridview - 如何按字母顺序对 GridView 项目进行分组 : Windows Store App

转载 作者:行者123 更新时间:2023-12-01 11:39:09 25 4
gpt4 key购买 nike

我想按字母顺序对 GridViewItems 进行分组,并将字母显示为组标题。
我需要

  • GroupItem 的标题
  • 团队风格
  • 项目面板的样式
  • GridViewItem 模板

并解释如何将分组的项目添加到 CollectionViewSource。

最佳答案

GridView 分组可以使用 CollectionViewSource 完成。在您的 Xaml 中,在页面的资源部分添加一个 CollectionViewSource。确保 IsSourceGrouped 设置为 true:

<Page.Resources>
<CollectionViewSource x:Name="FruitsCollectionViewSource" IsSourceGrouped="True"/>
</Page.Resources>

我们可以稍后在代码隐藏中设置CollectionViewSourceSource

然后 CollectionViewSource(FruitsCollectionViewSource) 应该设置为您的 GridView

ItemsSource
 <GridView
ItemsSource="{Binding Source={StaticResource FruitsCollectionViewSource}}"
x:Name="FruitGridView"
Padding="30,20,40,0"
SelectionMode="None"
IsSwipeEnabled="false"
IsItemClickEnabled="True"
ItemClick="FruitGridView_ItemClick">

//// GridView ItemTemplate

<GridView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Left" Width="250" Height="250">
<Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}">
<Image Source="{Binding Path=FruitImageSource}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
</Border>
<StackPanel VerticalAlignment="Bottom" Background="{ThemeResource ListViewItemOverlayBackgroundThemeBrush}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Fruit Name" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextBlockStyle}" Height="30" Margin="15,0,15,0"/>
<TextBlock Text="{Binding Path=FruitName}" Style="{StaticResource TitleTextBlockStyle}" Height="30" Margin="15,0,15,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Fruit Rate" Foreground="{ThemeResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap" Margin="15,0,87,10"/>
<TextBlock Text="{Binding Path=FruitRate}" Foreground="{ThemeResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>

////Group Style

<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text='{Binding Key}' Foreground="Gray" Margin="5" FontSize="30" FontFamily="Segoe UI Light" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid MaximumRowsOrColumns="2" Orientation="Horizontal" />
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle>

//// Item panel Style

<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid GroupPadding="0,0,70,0" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>

现在您可以从代码隐藏向您的 FruitList 添加项目

创建一个ObservableCollection FruitList

public ObservableCollection<Fruit> FruitList { get; set; }

创建一个模型类 Fruit

    public class Fruit
{
public string FruitName { get; set; }
public string FruitImageSource { get; set; }
public string FruitRate { get; set; }
}

初始化FruitList并将项目添加到FruitList

    FruitList.Add(new Fruit { FruitName = "Blackberry", FruitImageSource = "../Assets/blackberry.jpg", FruitRate = "150" });
FruitList.Add(new Fruit { FruitName = "Apple", FruitImageSource = "../Assets/apple.jpg", FruitRate = "150" });
FruitList.Add(new Fruit { FruitName = "Orange", FruitImageSource = "../Assets/orange.jpg", FruitRate = "250" });
FruitList.Add(new Fruit { FruitName = "Bilberry", FruitImageSource = "../Assets/bilberry.jpg", FruitRate = "250" });
FruitList.Add(new Fruit { FruitName = "Banana", FruitImageSource = "../Assets/banana.jpg", FruitRate = "50" });
FruitList.Add(new Fruit { FruitName = "Amla", FruitImageSource = "../Assets/amla.jpg", FruitRate = "120" });

现在你可以设置你的源 CollectionViewSource为此,我们首先需要对我们的 FruitList 进行排序。然后使用此 GetGroupsByLetter() 方法按升序或降序对水果名称进行分组。

        FruitList = new ObservableCollection<Fruit>(FruitList .OrderBy(c => c.FruitName));
FruitsCollectionViewSource.Source = GetGroupsByLetter();

GetGroupsByLetter()FruitList 中的项目分组。

    internal List<GroupInfoList<object>> GetGroupsByLetter()
{
var groups = new List<GroupInfoList<object>>();

var query = from item in FruitList
orderby ((Fruit)item).FruitName
group item by ((Fruit)item).FruitName[0] into g
select new { GroupName = g.Key, Items = g };
foreach (var g in query)
{
var info = new GroupInfoList<object>();
info.Key = g.GroupName;
foreach (var item in g.Items)
{
info.Add(item);
}

groups.Add(info);
}

return groups;
}

组信息列表:

    public class GroupInfoList<T> : List<object>
{
public object Key { get; set; }

public new IEnumerator<object> GetEnumerator()
{
return (System.Collections.Generic.IEnumerator<object>)base.GetEnumerator();
}
}

现在你已经完成了。

您的 GridView 将如下所示

enter image description here

关于gridview - 如何按字母顺序对 GridView 项目进行分组 : Windows Store App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23495024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com