void Grid1_Filtering(object sender, DataGridFilteringEventArgs e) { // Here we could prepare some data or even cancel the filtering process. Dispatcher.BeginInvoke(new Action(Grid1_Filtered)); }
/// <summary> /// Evaluates the current filters and applies the filtering to the collection view of the items control. /// </summary> private void EvaluateFilter() { if (_deferFilterEvaluationTimer != null) _deferFilterEvaluationTimer.Stop(); var collectionView = _dataGrid.Items; // Collect all active filters of all known columns. var filters = _filterColumnControls.Where(column => column.IsVisible && column.IsFiltered).ToArray(); if (Filtering != null) { // Notify client about additional columns being filtered. var columns = filters.Select(filter => filter.Column).Where(column => column != null).ToArray(); var newColumns = columns.Except(_filteredColumns).ToArray(); if (newColumns.Length > 0) { var args = new DataGridFilteringEventArgs(newColumns); Filtering(_dataGrid, args); if (args.Cancel) { return; } } _filteredColumns = columns; } if (FilterChanged != null) { FilterChanged(this, EventArgs.Empty); } try { // Apply filter to collection view if (!filters.Any()) { collectionView.Filter = _globalFilter; } else if (_globalFilter == null) { collectionView.Filter = item => filters.All(filter => filter.Matches(item)); } else { collectionView.Filter = item => _globalFilter(item) && filters.All(filter => filter.Matches(item)); } // Notify all filters about the change of the collection view. _filterColumnControls.ForEach(filter => filter.ValuesUpdated()); } catch (InvalidOperationException) { // InvalidOperation Exception: "'Filter' is not allowed during an AddNew or EditItem transaction." // Grid seems to be still in editing mode, even though we have called DataGrid.CommitEdit(). // Found no way to fix this by code, but after changing the filter another time by typing text it's OK again! // Very strange! } }