How do I 开发者_运维百科catch Filter event of CollectionViewSource in ViewModel using MVVM light?
Not familiar with MVVM Light however I am pretty sure what you are talking about is standard WPF framework bits.
The Filter
property on your ICollectionView
is a Predicate<object
> which you can set to a given method on your ViewModel which will be called each time the Filter
needs exercised.
One way to achieve this is by defining your ICollectionView
as a property within the ViewModel which is being bound to within your View.
private ICollectionView _view;
public ICollectionView Data
{
get
{
if (_view == null)
{
_view = CollectionViewSource.GetDefaultView(someCollection);
_view.Filter = Filter;
}
return _view;
}
}
private bool Filter(object arg)
{
//arg is the object being filtered on to make the decision of
//it being included in the returned ICollectionView
return true;
}
This allows all logic to remain in the ViewModel which I believe is your ultimate goal.
You might be interested in the BookLibrary sample application which delegates the Filter of a CollectionViewSource to the ViewModel. However, it doesn't use MVVM Light. The sample is part of the WPF Application Framework (WAF).
精彩评论