I have an ObservableCollection defined as
public ObservableCollection<KeyValuePair<string, double>> comboBoxSelections
{
get;
set;
}
later on in my code I need to iterate the collection and change some values only but keep the same key. I’ve tried the following
for (int i = 0; i < comboBoxSelections.Count ; i++)
{
comboBoxSelections[i].Value = SomeDoubleValue;
}
but this gives error Property or indexer 'System.Collections.Generic.KeyValuePair<string,double>.Value' cannot be assigned to -- it is read only
Can someone please explain why I get the error and how to allow updates to the ObservableCollection?
It’s not the
ObservableCollection<T>that is read-only, it’s theKeyValuePair<TKey, TValue>, because the latter is astruct. It is a good design practice forstructs to be immutable.The correct way to update your collection is