Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If your looking for something like this:</p> <p><a href="http://www.bendewey.com/code/GroupingSum.png">WPF Grouping with Total Sums http://www.bendewey.com/code/GroupingSum.png</a></p> <p>Then you can use the Template property of the ContainerStyle for the GroupStyle. In this example I use a DockPanel, with the Grid you supplied Docked on the bottom and an ItemsPresenter filling the remainder. In addition in order to get the Items totaled you'd have to use a Converter, which is supplied at the bottom.</p> <h2>Window.xaml</h2> <pre><code>&lt;Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="WpfApplication1.Window3" x:Name="Window" Title="Window3" xmlns:local="clr-namespace:WpfApplication1" Width="640" Height="480"&gt; &lt;Window.Resources&gt; &lt;local:MyDataSource x:Key="MyData" /&gt; &lt;CollectionViewSource x:Key="ViewSource" Source="{Binding Source={StaticResource MyData}, Path=Users}"&gt; &lt;CollectionViewSource.GroupDescriptions&gt; &lt;PropertyGroupDescription PropertyName="Country" /&gt; &lt;/CollectionViewSource.GroupDescriptions&gt; &lt;/CollectionViewSource&gt; &lt;/Window.Resources&gt; &lt;Grid x:Name="LayoutRoot"&gt; &lt;ListView ItemsSource="{Binding Source={StaticResource ViewSource}}"&gt; &lt;ListView.GroupStyle&gt; &lt;GroupStyle&gt; &lt;GroupStyle.ContainerStyle&gt; &lt;Style TargetType="{x:Type GroupItem}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type GroupItem}"&gt; &lt;DockPanel&gt; &lt;Grid DockPanel.Dock="Bottom"&gt; &lt;Grid.Resources&gt; &lt;local:TotalSumConverter x:Key="sumConverter" /&gt; &lt;/Grid.Resources&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Grid.Column="0" Text="Total: " FontWeight="Bold"/&gt; &lt;TextBlock Grid.Column="0" Text="{Binding Path=Name}" /&gt; &lt;/StackPanel&gt; &lt;Line Grid.Column="1" Stroke="Black" X2="500" Fill="Black" VerticalAlignment="Center" /&gt; &lt;TextBlock Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" Text="{Binding Path=Items, Converter={StaticResource sumConverter}}" /&gt; &lt;/Grid&gt; &lt;ItemsPresenter /&gt; &lt;/DockPanel&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/GroupStyle.ContainerStyle&gt; &lt;/GroupStyle&gt; &lt;/ListView.GroupStyle&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}"/&gt; &lt;GridViewColumn Width="140" Header="Phone Number" DisplayMemberBinding="{Binding Phone}"/&gt; &lt;GridViewColumn Width="140" Header="Country" DisplayMemberBinding="{Binding Country}" /&gt; &lt;GridViewColumn Width="140" Header="Total" DisplayMemberBinding="{Binding Total}" /&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <h2>MyDataSource.cs</h2> <pre><code>public class MyDataSource { public ObservableCollection&lt;User&gt; Users { get; set; } public MyDataSource() { Users = new ObservableCollection&lt;User&gt;(); LoadDummyData(); } private void LoadDummyData() { Users.Add(new User() { Name = "Frank", Phone = "(122) 555-1234", Country = "USA", Total = 432 }); Users.Add(new User() { Name = "Bob", Phone = "(212) 555-1234", Country = "USA", Total = 456 }); Users.Add(new User() { Name = "Mark", Phone = "(301) 555-1234", Country = "USA", Total = 123 }); Users.Add(new User() { Name = "Pierre", Phone = "+33 (122) 555-1234", Country = "France", Total = 333 }); Users.Add(new User() { Name = "Jacques", Phone = "+33 (122) 555-1234", Country = "France", Total = 222 }); Users.Add(new User() { Name = "Olivier", Phone = "+33 (122) 555-1234", Country = "France", Total = 444 }); } } </code></pre> <h2>User.cs</h2> <pre><code>public class User { public string Name { get; set; } public string Phone { get; set; } public string Country { get; set; } public double Total { get; set; } } </code></pre> <h2>TotalSumConverter.cs</h2> <pre><code>public class TotalSumConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var users = value as IEnumerable&lt;object&gt;; if (users == null) return "$0.00"; double sum = 0; foreach (var u in users) { sum += ((User)u).Total; } return sum.ToString("c"); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new System.NotImplementedException(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload