C# ObservableCollection包含字典<;int,Dictionary<;整数,<;对象>&燃气轮机;不因变化而触发
我有一个C# ObservableCollection包含字典<;int,Dictionary<;整数,<;对象>&燃气轮机;不因变化而触发,c#,wpf,data-binding,C#,Wpf,Data Binding,我有一个observateCollection对象,类是这样的: public class SchedulesInMonth : INotifyPropertyChanged { private Dictionary<int, Dictionary<int, Schedule>> _schedules; public Dictionary<int, Dictionary<int, Schedule>> Schedules {
observateCollection
对象,类是这样的:
public class SchedulesInMonth : INotifyPropertyChanged
{
private Dictionary<int, Dictionary<int, Schedule>> _schedules;
public Dictionary<int, Dictionary<int, Schedule>> Schedules
{
get => _schedules; set
{
_schedules = value;
OnPropertyChanged("Schedules");
}
}
// other property and method not related to this problem
// skipped for simplicity
}
即使ObservableCollection已更改,datagrid也不会更新
编辑:
一旦我发现,如果我这样做:
ScheduleInMonths[row].Schedules[row].Clear();
var temp = ScheduleInMonths[row].Schedules;
temp[col].Clear();
ScheduleInMonths[row].Schedules = temp;
绑定到ObservableCollection的数据网格已更新。此处实际上没有ObservableCollection。这是WPF中的一种特定类型,具有许多有用的行为。您只有一个符合INotifyPropertyChanges接口的属性(我假设您已经实现了这个接口,即使您的代码没有显示它) 第二个有效的原因是因为您正在重新设置字典。。。实际上,您需要将其分配给某个对象以使其触发。只有在分配新词典时,而不是在更改现有词典中的值时,才能获得更新。不幸的是,没有内置的“observedictionary”类,所以这里没有很多好的选项。在这个问题中,有很多关于如何做到这一点的有用建议: