如何从字典创建gridview [英] how to create gridview from dictionary
问题描述
我有一个字典,其中键是字符串,元素是列表
I have a dictionary where key is a String and element is a List
我想用元素中的元素从每个键创建一个组.但我不知道怎么做
I want to create a group from every key with elements from element. But I don`t know how to make it
<Page.Resources>
<!--
Collection of grouped items displayed by this page, bound to a subset
of the complete item list because items in groups cannot be virtualized
-->
<CollectionViewSource
x:Name="groupedItemsViewSource"
IsSourceGrouped="true"
/>
</Page.Resources>
<GridView ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
IsSwipeEnabled="True">
<GridView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding name}"
Foreground="White" />
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="Test 123" Foreground="Gold" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Vertical" />
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle>
</GridView>
在循环中我创建字典
groups.Add(this.letters[i], items);
在那之后我有
groupedItemsViewSource.Source = groups;
但我什么也没得到.我应该如何更正此问题以将键作为组标题并将每个列表作为此网格中的元素列表??
But I get nothing. How should I correct this to have a key as group title and every list as a list of elements in this grid ??
//编辑
好的,我发现制作 List> 而不是 Dictionary 更好.我现在有 3 个组标题(因为我的列表中有 3 个列表)但没有项目.海事组织这比第一个更好的方式
Ok I found that making List> instead of Dictionary is better. I get now 3 group headers (cause I got 3 lists in my list) but have no items. IMO this is better way than the first
//编辑 2我没有看到项目,因为背景和前景是白色的.愚蠢的我:) 但现在我有最后一个问题要解决.如何动态设置群组标题?
// EDIT 2 I didn`t see items cause background and foreground were white. Stupid me :) But now I have last problem to solve. How to dynamicly set groups titles ?
推荐答案
您实际上不必创建自定义类来启用分组.事实上,你可以使用 LINQ 来为你做这件事:
You actually don't have to create custom classes to enable grouping. In fact you can use LINQ to do it for you:
var result = from act in Activities 组 act by act.Project 到 grporderby grp.Key select grp;
var result = from act in Activities group act by act.Project into grp orderby grp.Key select grp;
cvsActivities.Source = 结果;
cvsActivities.Source = result;
为了在 GridView 中显示分组项目,您必须使用 CollectionViewSource.仅将组设置为 GridView.ItemsSource 是行不通的.您必须设置 GridView.ItemsSource = a CollectionViewSource 并且 CollectionViewSource 必须指向组.您可能还必须设置 CollectionViewSource.IsSourceGrouped = true.
In order to show grouped items in a GridView you must use CollectionViewSource. Just setting the groups as the GridView.ItemsSource will not work. You must set GridView.ItemsSource = a CollectionViewSource and CollectionViewSource must point to the groups. You will also likely have to set CollectionViewSource.IsSourceGrouped = true.
这篇关于如何从字典创建gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!