The first thing you should be asking yourself is...
Why am I adding the sorting description to the CollectionViewSource and the filter to the View? Shouldn't I be adding them both to the same object?
The answer is YES!
To add filter logic to the CollectionViewSource
directly, you add an event handler for the Filter
event.
Straight from MSDN, here is an example
listingDataView.Filter += new FilterEventHandler(ShowOnlyBargainsFilter);
private void ShowOnlyBargainsFilter(object sender, FilterEventArgs e)
{
AuctionItem product = e.Item as AuctionItem;
if (product != null)
{
// Filter out products with price 25 or above
if (product.CurrentPrice < 25)
{
e.Accepted = true;
}
else
{
e.Accepted = false;
}
}
}
Now, as for why the filter is getting removed when you add a sort description.
When you add a SortDescription
to a CollectionViewSource
, behind the scenes it eventually hits this block of code.
Predicate<object> filter;
if (FilterHandlersField.GetValue(this) != null)
{
filter = FilterWrapper;
}
else
{
filter = null;
}
if (view.CanFilter)
{
view.Filter = filter;
}
Clearly, it is overwriting the Filter you set on the View.
In case you are still curious, here is the source code for CollectionViewSource.