假设我有以下对象:
MyCustomObject {
public int Id { get; set; } = new Random().Next();
public string Name { get; set; }
}
列表
private List<MyCustomObject> _mainList = new List<MyCustomObject>();
和绑定源
private BindingSource _mainBindingSource = new BindingSource();
然后,我将过滤列表定义为 BindingSource 的数据源
_mainBindingSource.DataSource = _mainList.Where(x => x.Name.Contains(filterInput.Text));
我的问题:
为什么有效
(_mainBindingSource.Current as MyCustomObject).Name = "Foo" // It also changes the object on _mainList
不是吗?
_mainBindingSource.RemoveCurrent(); // The item is removed from _mainBindingSource but not from _mainList
当我调用 _mainBindingSource.RemoveCurrent();使用未过滤的列表作为数据源,该项目也会从该列表中删除。