'Grouping data in UWP DataGrid
I am trying to display Information objects grouped by Context property with in the UWP DataGrid using the code below. The first method creates a ObservableCollection<GroupInfoCollection<Information>>(); which is set to the CollectionViewSource.Source as shown in second part. And the XAML used is shown at the very end.
I can see the group headings as Group in UI, but without any subitems. I am not sure what I am missing. Also the group headers are not showing the Context property name.
var query = from item in exception.Information
group item by item.Context into g
select new { GroupName = g.Key, Items = g };
var information = new ObservableCollection<GroupInfoCollection<Information>>();
foreach (var group in query)
{
var info = new GroupInfoCollection<Information>
{
Key = group.GroupName
};
foreach (var item in group.Items)
{
info.Items.Add(item);
}
information.Add(info);
}
return information;
CollectionViewSource groupedItems = new CollectionViewSource
{
IsSourceGrouped = true,
Source = information // returned from above method
};
InformationGrid.ItemsSource = groupedItems.View;
<controls1:DataGrid x:Name="InformationGrid" AutoGenerateColumns="False" >
<controls1:DataGrid.Columns>
<controls1:DataGridTemplateColumn Header="Name" Width="48">
<controls1:DataGridTemplateColumn.CellTemplate>
<DataTemplate x:DataType="jama:Information">
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph="{x:Bind local:JamaOptionPage.GetGlyph(Level)}"/>
</DataTemplate>
</controls1:DataGridTemplateColumn.CellTemplate>
</controls1:DataGridTemplateColumn>
<controls1:DataGridTemplateColumn Header="Name" Width="220">
<controls1:DataGridTemplateColumn.CellTemplate>
<DataTemplate x:DataType="jama:Information">
<HyperlinkButton Content="{x:Bind Name}" NavigateUri="{x:Bind Link}" />
</DataTemplate>
</controls1:DataGridTemplateColumn.CellTemplate>
</controls1:DataGridTemplateColumn>
<controls1:DataGridTextColumn Header="Error" Binding="{Binding Message}"/>
</controls1:DataGrid.Columns>
</controls1:DataGrid>
Solution 1:[1]
I made two mistakes. My implementation of GroupInfoCollection was bit flawed. Instead of
public class GroupInfoCollection<T>
{
public string Key { get; set; }
public List<T> Items { get; set; } = new List<T>();
}
it should be
public class GroupInfoCollection<T>
{
public object Key { get; set; }
public new IEnumerator<T> GetEnumerator()
}
Also, the header was coming empty, for which one require something like
InformationGrid.LoadingRowGroup += InformationGridOnLoadingRowGroup;
private void InformationGridOnLoadingRowGroup(object sender, DataGridRowGroupHeaderEventArgs e)
{
var group = e.RowGroupHeader.CollectionViewGroup;
var item = group.GroupItems[0] as Information;
e.RowGroupHeader.PropertyValue = item.Context;
}
Solution 2:[2]
This implementation of GroupInfoCollection works for me
public class GroupInfoCollection<T> : ObservableCollection<T>
{
public object Key { get; set; }
public new IEnumerator<T> GetEnumerator()
{
return (IEnumerator<T>)base.GetEnumerator();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | resp78 |
| Solution 2 | Wanderson López |
