我有一个绑定到datagrid的ItemsSource的BindableCollection。将新项目添加到集合时,datagrid会更新。但是,当我修改集合中的项目时,即使我在CM中使用NotifyOfPropertyChange方法,datagrid也不会更新。
public BindableCollection<BaseDisplayData> DisplayData { get; private set; }
这有效:
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);
}
}
这不起作用:
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);
}
}
绑定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>