最初の例ではcomboBoxes itemsSourceを更新しないのはなぜですか?私が知る限り、明示的にOnPropertyChanged()を呼び出すと、GUIに通知され、VMから新しい値が取得されます。コンボボックスのitemssourceを辞書でバインドする
例1 - GUIに(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);
例2 - GUIの更新(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();
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);
注意を使用する必要があります。 UIが更新されないのは、実際のディクショナリインスタンスが変更されていないため、Bindingターゲットによって変更通知が無視されるためです。それに加えて、ObservableDictionaryの実装もインターネット上で見つけることができます。 – Clemens