Bingoogle,你会找到答案。http://code.msdn.microsoft.com/windowsapps/GroupedGridView-77c59e8e
SampleDataSource
示例应用程序中有这个方法:
internal List<GroupInfoList<object>> GetGroupsByLetter()
{
List<GroupInfoList<object>> groups = new List<GroupInfoList<object>>();
var query = from item in Collection
orderby ((Item)item).Title
group item by ((Item)item).Title[0] into g
select new { GroupName = g.Key, Items = g };
foreach (var g in query)
{
GroupInfoList<object> info = new GroupInfoList<object>();
info.Key = g.GroupName;
foreach (var item in g.Items)
{
info.Add(item);
}
groups.Add(info);
}
return groups;
}
}
如果您查看 Scenario2.xaml.cs - 它会调用该方法并将结果分配给 a CollectionViewSource
:
List<GroupInfoList<object>> dataLetter = _storeData.GetGroupsByLetter();
// sets the CollectionViewSource in the XAML page resources to the data groups
cvs2.Source = dataLetter;
cvs2
在页面的 XAML 代码中定义为资源:
<common:LayoutAwarePage.Resources>
<CollectionViewSource x:Name="cvs2" IsSourceGrouped="true" />
</common:LayoutAwarePage.Resources>
然后GridView
将CollectionViewSource
用作其ItemsCollection
并为DataTemplates
组和项目定义 :
<GridView x:Name="ItemsByLetter" VerticalAlignment="Bottom"
Height="325"
Width="1150"
ItemsSource="{Binding Source={StaticResource cvs2}}"
ShowsScrollingPlaceholders="False"
ContainerContentChanging="ItemsByLetter_ContainerContentChanging"
BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" BorderThickness="1">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid GroupHeaderPlacement="Left" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate>
<local:ItemViewer/>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Margin="10">
<TextBlock Text='{Binding Key}' Foreground="{StaticResource ApplicationForegroundThemeBrush}" FontSize="25" Margin="5" />
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</GridView.GroupStyle>
</GridView>