我有一个可以过滤的项目列表。Item 类有一个属性 IsChecked。有时,如果我在过滤列表时更改此属性(在运行时通过复选框),它不会在我的源集合中更改它。我做错了吗?
public class ViewModel
{
private ObservableCollection<Item> _items;
public ListCollectionView ItemsView { get; private set; }
private bool _checkAll;
public bool CheckAll
{
get
{
return _checkAll;
}
set
{
if (_checkAll == value)
return;
_checkAll = value;
if (_checkAll)
{
foreach (Item i in ItemsView )
i.IsChecked = true;
}
else
{
foreach (Item i in ItemsView )
i.IsChecked = false;
}
OnPropertyChanged("CheckAll");
}
}
public ViewModel()
{
_items= new ObservableCollection<Item>(this.createList());
ItemsView = CollectionViewSource.GetDefaultView(_items) as ListCollectionView;
ItemsView.Filter = filter;
}
private void filter(object obj)
{
...
}
}
public class Item
{
private bool _isChecked;
public Item()
{
}
public bool IsChecked
{
get {return _isChecked; }
set
{
if (_isChecked == value)
return;
_isChecked = value;
OnPropertyChanged("IsChecked");
}
}
...
}