Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The proper way for grouping is to use a <code>CollectionView</code> (for more details: <a href="http://www.wpftutorial.net/dataviews.html#grouping" rel="noreferrer">How to Navigate, Group, Sort and Filter Data in WPF</a>). The following is a simple proof of concept application I created for you to show you how to use a <code>CollectionView</code> for grouping you data:</p> <p>This class represents a row in the DataGrid:</p> <pre><code>public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public string Street { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string Country { get; set; } public string Image { get; set; } } </code></pre> <p>MaindWindow code behind:</p> <pre><code> /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // Create some test data var employees = new ObservableCollection&lt;Employee&gt; { new Employee {FirstName = "Mohammed", LastName = "Fadil", Street = "A B C", ZipCode = "123", City = "London", Country = "UK", Image = "/Images/globe.png"}, new Employee {FirstName = "Siraj", LastName = "Hussam", Street = "A B C", ZipCode = "123", City = "London", Country = "UK", Image = "/Images/globe.png"}, new Employee {FirstName = "Ayman", LastName = "Tariq", Street = "A B C", ZipCode = "123", City = "London", Country = "UK", Image = "/Images/globe.png"}, new Employee {FirstName = "Khalid", LastName = "Sheik", Street = "X Y Z", ZipCode = "234", City = "Paris", Country = "France", Image = "/Images/monitor.png"}, new Employee {FirstName = "Hassan", LastName = "Ali", Street = "Q W E R", ZipCode = "544", City = "NY", Country = "USA", Image = "/Images/star.png"}, new Employee {FirstName = "Ehsan", LastName = "Mahmoud", Street = "A B C", ZipCode = "123", City = "London", Country = "UK", Image = "/Images/globe.png"}, new Employee {FirstName = "Idris", LastName = "Sheik", Street = "X Y Z", ZipCode = "234", City = "Paris", Country = "France", Image = "/Images/monitor.png"}, new Employee {FirstName = "Khalil", LastName = "Ali", Street = "Q W E R", ZipCode = "544", City = "NY", Country = "USA", Image = "/Images/star.png"} }; ICollectionView employeesView = CollectionViewSource.GetDefaultView(employees); // Set the grouping by city proprty employeesView.GroupDescriptions.Add(new PropertyGroupDescription("City")); // Set the view as the DataContext for the DataGrid EmployeesDataGrid.DataContext = employeesView; } } </code></pre> <p>The DataGrid XAML code:</p> <pre><code> &lt;DataGrid Name="EmployeesDataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Binding="{Binding FirstName}" Header="First Name"/&gt; &lt;DataGridTextColumn Binding="{Binding LastName}" Header="Last Name"/&gt; &lt;DataGridTemplateColumn Header="Image"&gt; &lt;DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;Image Source="{Binding Image}"/&gt; &lt;/DataTemplate&gt; &lt;/DataGridTemplateColumn.CellTemplate&gt; &lt;/DataGridTemplateColumn&gt; &lt;/DataGrid.Columns&gt; &lt;DataGrid.GroupStyle&gt; &lt;GroupStyle&gt; &lt;GroupStyle.HeaderTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel&gt; &lt;TextBlock Text="{Binding Path=City}" FontWeight="Bold" Padding="3"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/GroupStyle.HeaderTemplate&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;Expander&gt; &lt;Expander.Header&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Path=Name}" /&gt; &lt;TextBlock Text="{Binding Path=ItemCount}" Margin="8,0,4,0"/&gt; &lt;TextBlock Text="Element(s)"/&gt; &lt;/StackPanel&gt; &lt;/Expander.Header&gt; &lt;ItemsPresenter /&gt; &lt;/Expander&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/GroupStyle.ContainerStyle&gt; &lt;/GroupStyle&gt; &lt;/DataGrid.GroupStyle&gt; &lt;/DataGrid&gt; </code></pre> <p>The result is:</p> <p><img src="https://i.stack.imgur.com/QQXB9.png" alt="Result"></p> <p>For more information about styling the DataGrid groups please check this post: <a href="http://www.wpftutorial.net/datagrid.html#grouping" rel="noreferrer">WPF DataGrid Control > Grouping</a></p>
 

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