/// <summary>
        /// Evaluates the current filters and applies the filtering to the collection view of the items control.
        /// </summary>
        private void EvaluateFilter()
        {
            _deferFilterEvaluationTimer?.Stop();

            var collectionView = DataGrid.Items;

            // Collect all active filters of all known columns.
            var filteredColumns = GetFilteredColumns();

            if (Filtering != null)
            {
                // Notify client about additional columns being filtered.

                var newColumns = filteredColumns
                                 .Except(_filteredColumns)
                                 .ToArray();

                if (newColumns.Length > 0)
                {
                    var columns = newColumns.ExceptNullItems().ToList().AsReadOnly();
                    var args    = new DataGridFilteringEventArgs(columns);
                    Filtering(DataGrid, args);

                    if (args.Cancel)
                    {
                        return;
                    }
                }

                _filteredColumns = filteredColumns;
            }

            FilterChanged?.Invoke(this, EventArgs.Empty);

            try
            {
                // Apply filter to collection view
                collectionView.Filter = CreatePredicate(filteredColumns);

                // Notify all filters about the change of the collection view.
                foreach (var control in FilterColumnControls)
                {
                    control?.ValuesUpdated();
                }

                var selectedItem = DataGrid.SelectedItem;
                if (selectedItem != null)
                {
                    DataGrid.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(() => DataGrid.ScrollIntoView(selectedItem)));
                }
            }
            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!
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Evaluates the current filters and applies the filtering to the collection view of the items control.
        /// </summary>
        private void EvaluateFilter()
        {
            _deferFilterEvaluationTimer?.Stop();

            var collectionView = _dataGrid.Items;

            // Collect all active filters of all known columns.
            var columnFilters = GetColumnFilters();

            if (Filtering != null)
            {
                // Notify client about additional columns being filtered.
                var columns = columnFilters
                              .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;
            }

            FilterChanged?.Invoke(this, EventArgs.Empty);

            try
            {
                // Apply filter to collection view
                collectionView.Filter = CreatePredicate(columnFilters);

                // Notify all filters about the change of the collection view.
                foreach (var control in _filterColumnControls)
                {
                    control.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!
            }
        }
        /// <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!
            }
        }