好的,我有这个KeyValuePair列表:
public ObservableCollection<KeyValuePair<string, float>> values { get; set; }
现在我想知道是否已有此元素。我试过这个:
bool conaintsValue = values.Contains(value);
但是我现在如何替换这个元素呢? 这就是我试过的:
if(values.Contains(value))
{
int i = values.IndexOf(value);
values[i] = value;
}
但这不起作用。
编辑: 我添加这样的值!值=列表!
KeyValuePair<string, float> value = new KeyValuePair<string, float>(chartKey, chartValue);
这是我的清单:
public ObservableCollection<KeyValuePair<string, float>> values { get; set; }
答案 0 :(得分:3)
我试过了:
if (values.Contains(value))
{
int i = values.IndexOf(value);
KeyValuePair<string, float> newValue = new KeyValuePair<string, float>("test", (float)11.3);
values[i] = newValue;
}
它有效。您应该使用不具有相同值的新值替换旧值。