How can I bind DataGrid row count after a CollectionView filter is applied?

本小妞迷上赌 提交于 2019-12-24 00:52:10

问题


Previously to implementing filtering on the CollectionView, i used this to display the number of rows within the datagrid;

<TextBlock Text="{Binding ElementName=dataGrid1, Path=ItemsSource.Count}" />

I have since implemented a filter as so;

ICollectionView cvs = CollectionViewSource.GetDefaultView(datagrid1.ItemsSource);
cvs.Filter = new Predicate<object>(FilterMethod);
...
public bool FilterMethod(object item) {
    // conditionally returns true/false
}

The TextBlock above still shows the unfiltered count, when visually the filter is removing items from view. How can I get the TextBlock above to show the row count as a result of the filter?


回答1:


The problem is that the filter is applied only on the collection view, and not on the item source.

You can do one of 2 thing:

  1. bind to the collection view count (make sure its INPC).
  2. bind to the items control items.count property.

This way the count will be same as what the user see.



来源:https://stackoverflow.com/questions/4609953/how-can-i-bind-datagrid-row-count-after-a-collectionview-filter-is-applied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!