C# 更改ObservableCollection值将引发只读错误

C# 更改ObservableCollection值将引发只读错误,c#,C#,我有一个可观察的集合,定义为 public ObservableCollection<KeyValuePair<string, double>> comboBoxSelections { get; set; } public-observeCollection组合框选项 { 得到; 设置 } 稍后在我的代码中,我需要迭代集合,只更改一些值,但保留相同的键。我试过以下方法 for (int i = 0; i < comboBoxSel

我有一个可观察的集合,定义为

public ObservableCollection<KeyValuePair<string, double>> comboBoxSelections 
{ 
 get; 
 set; 
}
public-observeCollection组合框选项
{ 
得到;
设置
}
稍后在我的代码中,我需要迭代集合,只更改一些值,但保留相同的键。我试过以下方法

        for (int i = 0; i < comboBoxSelections.Count ; i++)
        {
            comboBoxSelections[i].Value = SomeDoubleValue;
        }
for(int i=0;i
但这会导致错误
属性或索引器'System.Collections.Generic.KeyValuePair.Value'无法分配到--它是只读的


有人能解释一下我为什么会出现错误,以及如何允许对
可观察集合进行更新吗?

嗯,错误信息很清楚。
KeyValuePair
Value
属性是只读的。对于问题的第二部分,我无法给出详细答案,但快速谷歌搜索给出了:


嗯,错误信息很清楚。
KeyValuePair
Value
属性是只读的。对于问题的第二部分,我无法给出详细答案,但快速谷歌搜索给出了:


只读的不是
可观察集合
,而是
键值对
,因为后者是一个
结构
struct
s不可变是一种很好的设计实践

更新收藏的正确方法是

comboBoxSelections[i] =
    new KeyValuePair<string, double>(comboBoxSelections[i].Key, someDoubleValue);
comboBoxSelections[i]=
新的KeyValuePair(comboBoxSelections[i].Key,someDoubleValue);

只读的不是
可观察集合
,而是
键值对
,因为后者是一个
结构。
struct
s不可变是一种很好的设计实践

更新收藏的正确方法是

comboBoxSelections[i] =
    new KeyValuePair<string, double>(comboBoxSelections[i].Key, someDoubleValue);
comboBoxSelections[i]=
新的KeyValuePair(comboBoxSelections[i].Key,someDoubleValue);