Filter a DataGrid on a Text box

前端 未结 4 1971
挽巷
挽巷 2020-12-03 01:12

I search an example or sample to filter the WPF DataGrid column elements by a textbox.

\"alt

S

4条回答
  •  爱一瞬间的悲伤
    2020-12-03 01:59

    I have written my own FilterDataGrid Control, it's much more flexible than the ones provided on CodeProject or elsewhere. I can neither post the full code here, nor can I publish it.

    But: Since your datasource is most likely wrapped into a ICollectionView, you can do something like this:

        public void ApplyFilters()
        {
            ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
            if (view != null)
            {
                view.Filter = FilterPredicate; 
            }
        }
    
        private bool FilterPredicate(object item)
        {
            var yourBoundItemOrRow = item as BoundItemType;
    
            return aFilterObject.Matches(yourBoundItemOrRow);
        }
    

    You can implement any filter logic easily based on this concept. Even very, very powerful filters. Note: I have those methods in my own class derived from datagrid. They can be adapted to work outside of the grid, too, for example in a UserControl

提交回复
热议问题