質問

I have a Dictionary whose ValueCollection consists of classes, which is exposed as a property in my VM, and bound to a DataGrid on the UI. My problem is that the Dictionary is populated asynchronously (it's actually an ObservableDictionary, so I can monitor change events), and no results ever show up in the grid. I'm raising the PropertyChanged event in the VM for the property the DataGrid is bound to whenever an item is added to the Dictionary, but can't get any results. I have verified that the events do fire.

If I use a straight ObservableCollection instead of an ObservableDictionary, everything is fine, and I don't need to raise PropertyChanged even, but I really wanted to use Dictionary to hold my collection to keep a unique key on each entry.

Am I doing something wrong, or is there a workaround with a different collection that I could use? I've also tried calling .ToObservable() or .ToEnumerable() on the value collection for binding.

役に立ちましたか?

解決 2

The problem was solved by returning Dictionary.Values.ToList() in my VM instead of just .Values, .Values.GetEnumerator(), or .Values.ToObservable(). I did have to still raise PropertyChanged in my VM for the VM property returning the list.

他のヒント

The problem is that your ObservableDictionary probably implements INotifyCOllectionChanged (good), but your ObservableDictionary.Values does not.

Override the non generic IEnumerable.GetEnumerator method on your dictionary to return just the values instead of KeyValuePairs and bind the dictionary to your grid.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top