0

I have an ObservableCollection<SchedulesInMonth> object, the class is something like this:

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
}

the Schedules contain Dictionary of 28-31 schedules each day with a day in month as key, each schedules of a day is composed with 0-3 schedule(s), because the work shift is 3 each day.

If I do this:

ScheduleInMonths[row].Schedules[row].Clear();

The datagrid is not updated, even though the ObservableCollection is changed.

EDIT:

upon my discovery, if I do this:

var temp = ScheduleInMonths[row].Schedules;
temp[col].Clear();

ScheduleInMonths[row].Schedules = temp;

The datagrid that bounded to ObservableCollection is updated.

SIRS
  • 624
  • 6
  • 20

1 Answers1

0

You don't actually have an ObservableCollection here. That's a specific type in WPF that has a lot of useful behaviors. You just have a property that conforms to the INotifyPropertyChanges interface (which I assume you've implemented, even though your same code doesn't show it).

The reason the second one works is because you are setting the dictionary again... you actually need to assign it to something to get it to trigger. You only get the update when you assign a new dictionary not when you change a value inside the existing dictionary. Unfortunately, there is no built in "ObservableDictionary" class, so you don't have many good options here. There are lots of helpful suggestions for how to do that in this question: General Observable Dictionary Class for DataBinding/WPF C#