Reputation: 4641
How should I make my collection for grouped collection in gridview ?
When I create List<List<MyObject>>
groups I get grouped data with good count of groups and elements in each group but I have no group titles.
So I've created object MyGroupedObject that has two fields
public String Key;
public List<MyObject> items;
so I have grouped that in object. But when I pass it to the grid view (like below) I have nothing. No items, no groups no nothing
<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>
than in gridview i just bind this collection as ItemsSource
this question is based on: how to create gridview from dictionary
I've created new cause in the original is to many questions so I want to make it all clear
Upvotes: 1
Views: 2136
Reputation: 5633
Have you looked at the Grouping and Semantic Zoom sample? http://code.msdn.microsoft.com/windowsapps/groupedgridview-77c59e8e
You can also look at How to group items in a list or grid... http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh780627.aspx
Short explanation is... you can do it one of two ways...
List<Activity> Activities = new List<Activity>();
Activities.Add(new Activity()
{ Name = "Activity 1", Complete = true,
DueDate = startDate.AddDays(4), Project = "Project 1" });
Activities.Add(new Activity()
{ Name = "Activity 2", Complete = true,
DueDate = startDate.AddDays(5), Project = "Project 2" });
followed by the following to get the data in your CollectionViewSource
var result = from act in Activities
group act by act.Project into grp
orderby grp.Key select grp;
cvsActivities.Source = result;
Or you can build a hierarchy using object properties like this...
List<Project> Projects = new List<Project>();
Project newProject = new Project();
newProject.Name = "Project 1";
newProject.Activities.Add(new Activity()
{ Name = "Activity 1", Complete = true, DueDate = startDate.AddDays(4) });
newProject.Activities.Add(new Activity()
{ Name = "Activity 2", Complete = true, DueDate = startDate.AddDays(5) });
newProject.Activities.Add(new Activity()
{ Name = "Activity 3", Complete = false, DueDate = startDate.AddDays(7) });
newProject = new Project();
newProject.Name = "Project 2";
newProject.Activities.Add(new Activity()
{ Name = "Activity A", Complete = true, DueDate = startDate.AddDays(2) });
newProject.Activities.Add(new Activity()
{ Name = "Activity B", Complete = false, DueDate = startDate.AddDays(3) });
cvsProjects.Source = Projects;
You cannot bind to a simple list and have the grouping automatically configured for you.
Upvotes: 2
Reputation: 12465
Your Data should be just as described IList<MyGroupObject>
where MyGroupObject is defined as such
class MyGroupObject
{
public String Key; // used for group title
public IList<MyObject> Items;
}
Then your CollectionViewSource
should be defined as such
<CollectionViewSource
x:Name="groupedItemsViewSource"
IsSourceGrouped="true"
Source="{Binding GroupItems}" <!-- Property in the backing ViewModel -->
ItemsPath="Items" <!-- Property on the MyGroupObject class -->
/>
Upvotes: 1