【发布时间】:2020-06-01 11:15:51
【问题描述】:
我有两个连接的类:Smartphone 和 Model。 Smartphone 包含 Model 的集合,如下所示:
public class Smartphone
{
public string BrandName { get; set; }
public ObservableCollection<Model> Models { get; set; } = new ObservableCollection<Model>();
}
而Model:
public class Smartphone
{
public string ModelName { get; set; }
}
然后我在Model 类中添加了另一个属性:
public const string IsSelectPropertyName = "IsSelect";
private bool _isSelect = false;
public bool IsSelect
{
get
{
return _isSelect ;
}
set
{
Set(IsSelectPropertyName, ref _isSelect , value);
}
}
然后是Smartphone 类中的SelectAll:
private bool _selectAll;
public bool SelectAll
{
get
{
return _selectAll;
}
set
{
_selectAll = value;
foreach (var item in Models)
{
item.IsSelect = value;
}
Set(() => SelectAll, ref _selectAll, value);
}
}
这里的问题是,如果一项未被选中,SelectAll 仍然被选中。到目前为止,我尝试的是在 Smartphone 类中使用此功能:
public void CheckSelected()
{
bool isUnchecked = Models.Select(item => item.IsSelect).AsQueryable().All(value => value == false);
if (isUnchecked)
{
SelectAll = false;
} else
{
SelectAll = true;
}
}
但是,如果像这样添加到Model 类中的IsSelect 属性中:
public const string IsSelectPropertyName = "IsSelect";
private bool _isSelect = false;
public bool IsSelect
{
get
{
return _isSelect ;
}
set
{
Set(IsSelectPropertyName, ref _isSelect , value);
if (Smartphone != null)
{
Smartphone.CheckSelected();
}
}
}
我收到如下错误:
堆栈溢出异常
【问题讨论】:
标签: c# mvvm-light