Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

windows community toolkit - 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...