title: WPF Grouping using CollectionViewSource and DataBinding
body: <p>I'm binding a <code>CollectionViewSource</code> to a <code>ListView</code> to group items. It all works great except when I update the <code>ObservableCollection</code> the <code>CollectionViewSource</code> is based on. If I update a value of an object in the collection the UI is never updated. Here is an example:</p>
  
  <pre><code>&lt;ListView x:Name="MyListView" Margin="0,0,0,65"&gt;
      &lt;ListView.GroupStyle&gt;
          &lt;GroupStyle&gt;
              &lt;GroupStyle.ContainerStyle&gt;
                  &lt;Style TargetType="{x:Type GroupItem}"&gt;
                      &lt;Setter Property="Margin" Value="0,0,0,5"/&gt;
                      &lt;Setter Property="Template"&gt;
                          &lt;Setter.Value&gt;
                              &lt;ControlTemplate TargetType="{x:Type GroupItem}"&gt;
                                  &lt;Expander IsExpanded="true" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1"&gt;
                                      &lt;Expander.Header&gt;
                                          &lt;DockPanel&gt;
                                              &lt;TextBlock FontWeight="Bold" Text="{Binding Name}" Margin="5,0,0,0" Width="80"/&gt;
                                              &lt;TextBlock FontWeight="Bold" Width="60" TextAlignment="Center" Margin="16,0,0,0" Text="{Binding Items, Converter={StaticResource Converter2}}" /&gt;
                                          &lt;/DockPanel&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;/ListView.GroupStyle&gt;
      &lt;ListView.View&gt;
          &lt;GridView&gt;
              &lt;GridViewColumn Width="300" Header="Amount" &gt;
                  &lt;GridViewColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBlock Text="{Binding Amount}" Margin="80,0,0,0"/&gt;
                      &lt;/DataTemplate&gt;
                   &lt;/GridViewColumn.CellTemplate&gt;
              &lt;/GridViewColumn&gt;
          &lt;/GridView&gt;
      &lt;/ListView.View&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>You'll notice it's calling a converter in the group and giving it the items collection. This is so the converter can calculate the average of the rows and return that result:</p>
  
  <pre><code>public class AverageConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
          IEnumerable&lt;object&gt; rows = (IEnumerable&lt;object&gt;) value;
          double average = rows.Average(a =&gt; ((DisplayRow) a).Amount);
          return average;
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>In the code behind I add the rows and create the <code>CollectionViewSource</code>:</p>
  
  <p>private readonly ObservableCollection displayRows = new ObservableCollection();</p>
  
  <pre><code>public Window1()
  {
      InitializeComponent();
  
      displayRows.Add(new DisplayRow { Title = "Sales", Amount=16} );
      displayRows.Add(new DisplayRow { Title = "Marketing", Amount=14} );
      displayRows.Add(new DisplayRow { Title = "Technology", Amount=13} );
      displayRows.Add(new DisplayRow { Title = "Sales", Amount=11} );
      displayRows.Add(new DisplayRow { Title = "Marketing", Amount=13} );
      displayRows.Add(new DisplayRow { Title = "Technology", Amount=12} );
      CollectionViewSource viewSource = new CollectionViewSource { Source = displayRows };
      viewSource.GroupDescriptions.Add(new PropertyGroupDescription("Title"));
      MyListView.ItemsSource = viewSource.View;
  }
  </code></pre>
  
  <p>The <code>DisplayRow</code> object implements <code>INotifyPropertyChanged</code>, and is just a simple class.</p>
  
  <p>Everything works well and the display is the way I want, but if I change a value in the <code>ObservableCollection</code> the UI doesn't change. </p>
  
  <p>If I add an element to the collection I can see it appear on the screen but the converter is never called to recompute the average.  Any ideas?</p>