Click here to Skip to main content
15,895,746 members
Articles / Desktop Programming / WPF

Auto-filter for Microsoft WPF DataGrid

Rate me:
Please Sign up or sign in to vote.
4.81/5 (25 votes)
29 Jan 2009Eclipse3 min read 227.1K   8.4K   62  
Allows auto filtering functionality for DataGrid columns.
using System;
using Stepi.Collections;
using Stepi.Collections.Filters;
using System.ComponentModel;

namespace Stepi.UIFilters
{
    public class CompareFilterPresentationModel<T> : ICompareFilterPresentationModel<T>
        where T : IComparable
    {
        private static readonly PropertyChangedEventArgs _activeEventArgs = new PropertyChangedEventArgs("IsActive");
        private static readonly PropertyChangedEventArgs _applyEventArgs = new PropertyChangedEventArgs("CanApply");

        private bool _targetError;
        private bool _isActive;
        private bool _canApply = true;
        private readonly ICompareFilter<T> _filter;
        private readonly string _name;

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        public CompareFilterPresentationModel(ICompareFilter<T> compareFilter, string name)
        {
            if (compareFilter == null)
            {
                throw new ArgumentNullException("compareFilter");
            }
            _name = name;
            _filter = compareFilter;
        }

        public T Target
        {
            get
            {
                return _filter.CompareTo;
            }
            set
            {

                _filter.CompareTo = value;
            }
        }

        public string Name
        {
            get { return _name; }
        }

        object ICompareFilterPresentationModel.Target
        {
            get
            {
                return _filter.CompareTo;
            }
            set
            {
                _targetError = false;
                if (!(value is T))
                {
                    _targetError = true;
                    IsActive = false;
                    return;
                }

                _filter.CompareTo = (T)value;

            }
        }

        public IFilter Filter
        {
            get { return _filter; }
        }

        public bool CanApply
        {
            get { return _canApply; }
            set
            {
                if (_canApply != value)
                {
                    _canApply = value;
                    PropertyChanged(this, _applyEventArgs);
                }
            }
        }

        public bool IsActive
        {
            get
            {
                return _isActive;
            }
            set
            {
                if (_isActive != value)
                {
                    _isActive = value;
                    PropertyChanged(this, _activeEventArgs);
                }
            }
        }

        public void UpdateData(bool isDisplaying)
        { }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Eclipse Public License 1.0


Written By
Software Developer (Senior) Lab49
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions