C# 如何从字典创建gridview

C# 如何从字典创建gridview,c#,xaml,windows-8,windows-runtime,winrt-xaml,C#,Xaml,Windows 8,Windows Runtime,Winrt Xaml,我有一本字典,其中key是字符串,element是列表 我想从每个键创建一个组,其中包含来自元素的元素。但是我不知道怎么做 <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 virtual

我有一本字典,其中key是字符串,element是列表

我想从每个键创建一个组,其中包含来自元素的元素。但是我不知道怎么做

<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>
在那之后我有

groupedItemsViewSource.Source = groups;
但我什么也得不到。我应该如何更正这一点,使其将一个键作为组标题,并将每个列表作为该网格中的元素列表

//编辑

好的,我发现用列表>代替字典更好。我现在有3个组标题(因为我的列表中有3个列表),但没有项目。依我看,这是比第一种更好的方法

//编辑2
我没有看到项目,因为背景和前景是白色的。愚蠢的我:)但现在我有最后一个问题要解决。如何动态设置组标题?

字典未实现INotifyPropertyChanged或INotifyCollectionChanged,这是需要进行绑定工作时所必需的

public class yourclass
{
    public string Key { get; set; }
    public int Value { get; set; }
}


ObservableCollection<yourclass> dict = new ObservableCollection<MyCustomClass>();
dict.Add(new yourclass{Key = "yourkey", Value = whatyouwant});
dict.Add(new yourclass{ Key = "yourkey2", Value = whatyouwant });
公共类您的类
{
公共字符串密钥{get;set;}
公共int值{get;set;}
}
ObservableCollection dict=新的ObservableCollection();
Add(newyourclass{Key=“yourkey”,Value=whatyouwant});
dict.Add(newyourclass{Key=“yourkey2”,Value=whatyouwant});

字典未实现INotifyPropertyChanged或INotifyCollectionChanged,这是您需要进行绑定工作时所必需的

public class yourclass
{
    public string Key { get; set; }
    public int Value { get; set; }
}


ObservableCollection<yourclass> dict = new ObservableCollection<MyCustomClass>();
dict.Add(new yourclass{Key = "yourkey", Value = whatyouwant});
dict.Add(new yourclass{ Key = "yourkey2", Value = whatyouwant });
公共类您的类
{
公共字符串密钥{get;set;}
公共int值{get;set;}
}
ObservableCollection dict=新的ObservableCollection();
Add(newyourclass{Key=“yourkey”,Value=whatyouwant});
dict.Add(newyourclass{Key=“yourkey2”,Value=whatyouwant});

实际上,您不必创建自定义类来启用分组。事实上,您可以使用LINQ为您执行以下操作:

var结果=从“活动”组中的“行为”按行为进行。项目导入grp orderby grp。键选择grp

cvsActivities.Source=结果


为了在GridView中显示分组项目,必须使用CollectionViewSource。仅将组设置为GridView.ItemsSource将不起作用。必须设置GridView.ItemsSource=CollectionViewSource,CollectionViewSource必须指向组。您可能还需要设置CollectionViewSource.IsSourceGrouped=true。

您实际上不必创建自定义类来启用分组。事实上,您可以使用LINQ为您执行以下操作:

var结果=从“活动”组中的“行为”按行为进行。项目导入grp orderby grp。键选择grp

cvsActivities.Source=结果


为了在GridView中显示分组项目,必须使用CollectionViewSource。仅将组设置为GridView.ItemsSource将不起作用。必须设置GridView.ItemsSource=CollectionViewSource,CollectionViewSource必须指向组。您可能还需要将CollectionViewSource.IsSourceGrouped设置为true。

不要更改您的问题。它让我们朝一个移动的目标射击。@Erno抱歉,我只是想提供尽可能多的信息。这不是更多的信息,这只是另一个问题。想象一下,有些人的问题与您上次添加的问题相同。他在这个问题上绊倒的机会有多大?不要改变你的问题。它让我们朝一个移动的目标射击。@Erno抱歉,我只是想提供尽可能多的信息。这不是更多的信息,这只是另一个问题。想象一下,有些人的问题与您上次添加的问题相同。他在这个问题上出错的可能性有多大?谢谢你的帮助。我上过这种课。With key field and value witch为列表类型。但现在我在装订后什么也得不到。也许我可以给你发送更多的代码,这样你会有更好的概述?您好,看看这个项目,它将帮助您了解如何绑定网格控件不幸的是,我不能解决我的问题。当我只想将项目绑定到网格视图时,这很容易。但我不能绑定分组对象。例如,将键和列表作为项的对象。我什么也得不到。只是空白。无项目,无标题:/谢谢您的帮助。我上过这种课。With key field and value witch为列表类型。但现在我在装订后什么也得不到。也许我可以给你发送更多的代码,这样你会有更好的概述?您好,看看这个项目,它将帮助您了解如何绑定网格控件不幸的是,我不能解决我的问题。当我只想将项目绑定到网格视图时,这很容易。但我不能绑定分组对象。例如,将键和列表作为项的对象。我什么也得不到。只是空白。没有项目,没有标题:/