2017-08-01 6 views
0

最初の例では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); 

答えて

0

なしプロパティは、通知が作成される変更しましたディクショナリにアイテムを追加/削除するときに使用します。ただし、プロパティを再割り当てすると、OnPropertyChangedが起動され、GUIが更新されます。

コレクションが追加されたときにGUIを更新するためには、あなたはのObservableCollectionクラスにOPが明示的にPropertyChangedイベントを発生させていること https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx

http://www.c-sharpcorner.com/UploadFile/e06010/observablecollection-in-wpf/

+0

注意を使用する必要があります。 UIが更新されないのは、実際のディクショナリインスタンスが変更されていないため、Bindingターゲットによって変更通知が無視されるためです。それに加えて、ObservableDictionaryの実装もインターネット上で見つけることができます。 – Clemens

関連する問題