Reputation: 188
Why would the first example not update the comboBoxes itemsSource, but the second would? As far as I knew, if I explicitly called OnPropertyChanged() then it would notify the GUI and get the new value from my VM.
example 1 - doesn't update in GUI (no items in cbo)
// this is the property it's bound to
this.AvailableSleepTimes = new Dictionary<string, int>();
// gets a dictionary with values
Dictionary<string, int> allTimes = ReminderTimesManager.GetReminderTimes();
foreach(KeyValuePair<string,int> pair in allTimes)
{
// logic for adding to the dictionary
this.AvailableSleepTimes.Add(pair.Key, pair.Value);
}
OnPropertyChanged(() => this.AvailableSleepTimes);
example 2 - updates in GUI (cbo is filled)
// this is the property it's bound to
this.AvailableSleepTimes = new Dictionary<string, int>();
// gets a dictionary with values
Dictionary<string, int> allTimes = ReminderTimesManager.GetReminderTimes();
Dictionary<string, int> newList = new Dictionary<string, int>();
foreach(KeyValuePair<string,int> pair in allTimes)
{
// logic for adding to the dictionary
newList.Add(pair.Key, pair.Value);
}
this.AvailableSleepTimes = newList;
OnPropertyChanged(() => this.AvailableSleepTimes);
Upvotes: 1
Views: 509
Reputation: 729
No property changed notifications are created when you add/remove an item to the dictionary. However, when you reassign, the property, it fires the OnPropertyChanged and updates the GUI.
In order for the GUI to update when the collection is added to, you need to use the ObservableCollection class https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx
http://www.c-sharpcorner.com/UploadFile/e06010/observablecollection-in-wpf/
Upvotes: 1