JKoning
JKoning

Reputation: 285

Get sub totals in a XAML grouped grid view

I have a application where we show data in a grouped grid view. What is the best way to get subtotals per group the grouped grid view?

Upvotes: 0

Views: 29

Answers (1)

Sander van den Hoven
Sander van den Hoven

Reputation: 652

You can change the header text of each group with the number of items of the group.

Assuming that you have a GroupStyle

<GroupStyle>
    <GroupStyle.HeaderTemplate>
        <DataTemplate>
             <TextBlock Text='{Binding Key}' Foreground="{StaticResource ApplicationForegroundThemeBrush}" Margin="5" FontSize="18" FontFamily="Segoe UI" FontWeight="Light" />
        DataTemplate>
    </GroupStyle.HeaderTemplate>
 </GroupStyle>

In the creation of the list with the grouped data you can add the total of the group in the text of the key

public List<ItemList> CreateGroupedData()
{
    if (ReceivedList!= null)
    {
        var result =
        from t in ReceivedList
        group t by t.GroupField into g
        orderby g.Key
        select new { Key = g.Key, Items = g };

        List<ItemList> lists = new List<ItemList>();
        foreach (var i in result)
        {
            ItemList list = new ItemList();
            list.Key = $"{i.Key.ToString)} [{i.Items.Count.ToString()}]";
            lists.Add(list);
        }
        return lists;
    }
}

Upvotes: 1

Related Questions