title: How do I group items in a WPF ListView
body: <p>I have a <code>ListView</code> that I want to group results into, however the examples I am finding are not working. How can I group my results?</p>
  
  <p>I want to group on the <code>Status</code> property of a custom object.</p>
  
  <p>This is what I have:</p>
  
  <pre><code>&lt;ListView IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding}"
            HorizontalContentAlignment="Stretch"
            ScrollViewer.HorizontalScrollBarVisibility="Disabled"
            Background="Transparent" SelectionChanged="ListView_SelectionChanged"
            Name="lstShelvedOrders"&gt;
  
          &lt;ListView.GroupStyle&gt;
              &lt;GroupStyle&gt;
                  &lt;GroupStyle.HeaderTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBlock FontWeight="Bold" FontSize="15"
                           Text="{Binding Path=Status}"/&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/GroupStyle.HeaderTemplate&gt;
              &lt;/GroupStyle&gt;
          &lt;/ListView.GroupStyle&gt;
  
          &lt;ListView.ItemContainerStyle&gt;
              &lt;Style TargetType="{x:Type ListViewItem}"&gt;
                  &lt;Setter Property="BorderThickness" Value="1" /&gt;
                  &lt;Setter Property="Width" Value="Auto" /&gt;
                  &lt;Setter Property="FontSize" Value="10.4"  /&gt;               
              &lt;/Style&gt;
          &lt;/ListView.ItemContainerStyle&gt;
  
          &lt;ListView.View&gt;
              &lt;GridView&gt;
                  &lt;GridViewColumn DisplayMemberBinding="{Binding Path=Number}" Header="Shelve ID"  /&gt;
                  &lt;GridViewColumn DisplayMemberBinding="{Binding Path=Customer}" Header="Customer" /&gt;
                  &lt;GridViewColumn DisplayMemberBinding="{Binding Path=PurchaseOrderNo}" Header="PO Number" /&gt;
                  &lt;GridViewColumn DisplayMemberBinding="{Binding Path=SubmittedBy}" Header="Shelved By"  /&gt;
                  &lt;GridViewColumn DisplayMemberBinding="{Binding Path=OrderDate, StringFormat=MMM dd\, yyyy}" Header="Date"  /&gt;
                  &lt;GridViewColumn DisplayMemberBinding="{Binding Path=CustomerTerms.Description}" Header="Order Terms"  /&gt;
                  &lt;GridViewColumn DisplayMemberBinding="{Binding Path=ShippingMethod.Description}" Header="Shipping"  /&gt;
                  &lt;GridViewColumn DisplayMemberBinding="{Binding Path=TotalPrice, StringFormat=c}" Header="Order Total"  /&gt;
              &lt;/GridView&gt;
          &lt;/ListView.View&gt;
      &lt;/ListView&gt;
  </code></pre>
  
  <p>And this is the code that I have:</p>
  
  <pre><code> void ShelvedOrderList_DataContextChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
   {
       AddGrouping();
   }
  
   private void AddGrouping()
   {
       if ( lstShelvedOrders.ItemsSource == null)
       {
           return;
       }
  
       CollectionView myView = (CollectionView)CollectionViewSource.GetDefaultView(lstShelvedOrders.ItemsSource);
       PropertyGroupDescription groupDescription = new PropertyGroupDescription("Status");
       myView.GroupDescriptions.Add(groupDescription);
   }
  </code></pre>