gpt4 book ai didi

c# - .Select 不更新 View 模型对象的 ObservableCollection

转载 作者:太空宇宙 更新时间:2023-11-03 19:54:16 25 4
gpt4 key购买 nike

我有一个 ObservableCollection 类型的 FilterableListItem(如下),我进行以下查询,

this.Gears.Select(r => r.IsAvailible = r.Value == "X6");

但它不会更新列表中的任何项目。我如何使选择语句更新列表?

public class FilterableListItem : ViewModelBase
{
private string value;

private bool isAvailible;

public string Value
{
get
{
return this.value;
}
set
{
this.value = value;
this.OnPropertyChanged("Value");
}
}

public bool IsAvailible
{
get
{
return this.isAvailible;
}
set
{
if (value != this.isAvailible)
{
this.isAvailible = value;
this.OnPropertyChanged("IsVisible");
}
}
}

public override string ToString()
{
return this.Value;
}
}

最佳答案

正如我在评论中提到的,Select 不应用于更新您的对象,而应用于投影您集合中的每个元素。

您实际上应该使用标准的 foreach 来更新您的对象:

foreach (var item in Gears)
item.IsAvailable = (r.Value == "X6");

这将相应地更新您的项目。

关于c# - .Select 不更新 View 模型对象的 ObservableCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35557157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com