0

I have a BindableCollection that is bound to a datagrid's ItemsSource. When new items are added to the collection, the datagrid updates. However, when I modify an item in the collection, the datagrid does not update, even if I use the NotifyOfPropertyChange method in CM.

    public BindableCollection<BaseDisplayData> DisplayData { get; private set; }

This works:

void dataReportingService_TestStatusReceived(object sender, TestStatusEventArgs e)
        {
            BaseDisplayData match = DisplayData.SingleOrDefault(d => d.SampleID == e.SampleID && d.Test == e.Test);
            if (match != null)
            {
                match.Status = e.Status;
                DisplayData.Remove(match);
                DisplayData.Insert(0, match);
            }
        }

This does not work:

void dataReportingService_TestStatusReceived(object sender, TestStatusEventArgs e)
        {
            BaseDisplayData match = DisplayData.SingleOrDefault(d => d.SampleID == e.SampleID && d.Test == e.Test);
            if (match != null)
            {
                match.Status = e.Status;
                NotifyOfPropertyChange(() => DisplayData);
            }
        }

Binding XAML:

<DataGrid AutoGenerateColumns="False" Name="DisplayData" ItemsSource="{Binding DisplayData, NotifyOnSourceUpdated=True}" >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Sample ID" Width="Auto" Binding="{Binding SampleID}" />
        <DataGridTextColumn Header="H2 Ave." Width="*" Binding="{Binding HydrogenAverage, StringFormat=F1}" />
        <DataGridTextColumn Header="N2 Ave." Width="*" Binding="{Binding NitrogenAverage, StringFormat=F1}" />
        <DataGridTextColumn Header="O2 Ave." Width="*" Binding="{Binding OxygenAverage, StringFormat=F1}" />
        <DataGridTextColumn Header="Reporter" Width="*" Binding="{Binding ReporterID}" />
        <DataGridTextColumn Header="Test Status" Width="*" Binding="{Binding Status}" />
    </DataGrid.Columns>        
</DataGrid>
blueshift
  • 831
  • 2
  • 12
  • 24
  • possible duplicate of [ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)](http://stackoverflow.com/questions/1427471/observablecollection-not-noticing-when-item-in-it-changes-even-with-inotifyprop) – Noctis Sep 02 '14 at 00:12
  • Have a look at that answer, it should explain what's happening. – Noctis Sep 02 '14 at 00:12
  • But I am explicitly indicating that DisplayData has changed by using NotifyOfPropertyChange(() => DisplayData) -- I am not waiting for the collection to react to the change of one of its items. So, I don't understand why the datagrid does not update when I explicitly tell it that its ItemsSource has changed. – blueshift Sep 02 '14 at 02:07

0 Answers0