title: WPF Grouping a DataGrid nested in databound tabs
body: <p>I need to apply grouping to rows in a <code>DataGrid</code> that is nested in a <code>TabControl</code>. Both the <code>DataGrid</code> and the <code>TabControl</code> are databound.  The outermost control is bound to a ViewModel that exposes a collection of pages that is bound to the <code>TabControl</code>. Each page exposes a collection of lines that is bound to the <code>Grid</code>.</p>
  <p>I am tried to follow patterns like <a href="https://stackoverflow.com/questions/3826038/wpf-datagrid-grouping-does-not-show-up-due-to-binding-error">this one from SO</a> and <a href="http://www.c-sharpcorner.com/UploadFile/dpatra/548/Default.aspx" rel="nofollow noreferrer">this one from C-SharpCorner</a>.  I'm not married to these, so if there is a better pattern (<a href="https://stackoverflow.com/questions/3021198/mvvm-what-part-of-pattern-responsible-for-grouping-of-datagrid">as this post seems to indicate</a>), I'm willing to go in another direction.</p>
  <p>For now, I am not sure where to inject the definition of the <code>PropertyGroupDescription</code> and bind it to my desired <code>PropertyName</code>. I've tried doing it as a resource and directly as a <code>CollectionViewSource</code> on the <code>DataGrid</code>. But neither produced reasonable results.</p>
  <p>This is what I have so far:</p>
  <pre><code>&lt;UserControl x:Class=&quot;View.ViewInvoiceUserControl&quot;
           xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
           xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
           xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot; 
           xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot; 
           mc:Ignorable=&quot;d&quot; &gt;
  &lt;Grid Name=&quot;_grid&quot; MinHeight=&quot;100&quot;&gt;
  </code></pre>
  <blockquote>
  <p>snipped for clarity...</p>
  </blockquote>
  <pre><code>&lt;TabControl 
      ItemsSource=&quot;{Binding Path=CurrentInvoice.InvoicePages}&quot; 
      Grid.Column=&quot;0&quot; 
      Grid.ColumnSpan=&quot;4&quot; 
      Grid.Row=&quot;5&quot; 
      HorizontalAlignment=&quot;Left&quot; 
      Margin=&quot;0,4,0,0&quot; 
      Name=&quot;_invoicePageTabControl&quot; 
      VerticalAlignment=&quot;Top&quot;&gt;
      &lt;TabControl.ItemContainerStyle /&gt;
          &lt;TabControl.ContentTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;DataGrid 
                      Name=&quot;_invoiceLineGrid&quot; 
                      AutoGenerateColumns=&quot;False&quot; 
                      CanUserSortColumns=&quot;True&quot;
                      ColumnHeaderStyle=&quot;{StaticResource columnHeaderStyle}&quot;
                      HorizontalAlignment=&quot;Left&quot; 
                      ItemsSource=&quot;{Binding InvoiceLines}&quot; 
                      IsReadOnly=&quot;True&quot;
                      RowDetailsVisibilityMode=&quot;VisibleWhenSelected&quot;
                      RowStyle=&quot;{StaticResource DataGridRowStyle}&quot;
                      SelectionUnit=&quot;FullRow&quot;
                      VerticalAlignment=&quot;Top&quot;
                      Visibility=&quot;{Binding Path=InvoiceLineGridVisibility}&quot;
                      Initialized=&quot;_invoiceLineGrid_Initialized&quot;&gt;
                      &lt;DataGrid.Columns&gt;
                          &lt;DataGridTextColumn Header=&quot;Line Number&quot; MinWidth=&quot;50&quot;  Binding=&quot;{Binding InvoiceLineNumber}&quot; /&gt;
                          &lt;DataGridTextColumn Header=&quot;Description&quot; MinWidth=&quot;50&quot; Width=&quot;*&quot; Binding=&quot;{Binding Description}&quot; /&gt;
                          &lt;DataGridTextColumn Header=&quot;Quantity&quot; MinWidth=&quot;50&quot; Binding=&quot;{Binding Quantity}&quot; /&gt;
                          &lt;DataGridTextColumn Header=&quot;Extended Cost&quot; MinWidth=&quot;50&quot; Width=&quot;*&quot; Binding=&quot;{Binding ExtendedCost}&quot; /&gt;
                      &lt;/DataGrid.Columns&gt;
                      &lt;DataGrid.GroupStyle&gt;
                          &lt;GroupStyle&gt;
                              &lt;GroupStyle.ContainerStyle&gt;
                                  &lt;Style TargetType=&quot;{x:Type GroupItem}&quot;&gt;
                                      &lt;Setter Property=&quot;Template&quot;&gt;
                                          &lt;Setter.Value&gt;
                                              &lt;ControlTemplate TargetType=&quot;{x:Type GroupItem}&quot;&gt;
                                                  &lt;Expander&gt;
                                                      &lt;Expander.Header&gt;
                                                          &lt;StackPanel&gt;
                                                              &lt;TextBlock Text=&quot;{Binding Name}&quot; /&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;
              &lt;/DataTemplate&gt;
          &lt;/TabControl.ContentTemplate&gt;
      &lt;/TabControl&gt;
  &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  <p>I think I need to add something like:</p>
  <pre><code>&lt;CollectionViewSource&gt;
      &lt;CollectionViewSource.GroupDescriptions&gt;
          &lt;PropertyGroupDescription PropertyName=&quot;DepartmentBillingCode&quot; /&gt;
      &lt;/CollectionViewSource.GroupDescriptions&gt;
  &lt;/CollectionViewSource&gt;
  </code></pre>
  <p>If I add the <code>CollectionViewSource</code> as a resource and change the <code>DataGrid</code> binding, there are no rows in the grid.
  If I add the <code>CollectionViewSource</code> to the <code>DataGrid</code>, I get an exception: <code>Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.</code> nested in <code>Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception.</code>
  But I don't see a <code>GroupDescription</code> or <code>PropertyGroupDescription</code> on <code>DataGrid.ItemSource</code></p>
  <p>So, I'm feeling lost now.
  Any suggestions are appreciated.</p>
  <p>Thanks</p>