Click here to Skip to main content
11,716,249 members (90,597 online)
Click here to Skip to main content
Add your own
alternative version

Auto-filter for Microsoft WPF DataGrid

, 29 Jan 2009 Eclipse 106.8K 5.1K 53
Allows auto filtering functionality for DataGrid columns.
WPFGridFilter.zip
WPFGridFilter
PowerCollections
PowerCollections.csproj.user
Properties
Stepi.Collections
bin
Filters
Properties
Stepi.UIFilters
bin
Initializers
Properties
Settings.settings
Themes
Test.GridSorting
bin
Debug
Test.GridFilter.vshost.exe
Properties
Settings.settings
using System;
using System.Reflection;
using System.ComponentModel;
using System.Diagnostics;

namespace Stepi.Collections.Filters
{
    /// <summary>
    /// Defines the range filter 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class RangeFilter<T> : Filter, IRangeFilter<T>
        where T : IComparable
    {
        private static readonly PropertyChangedEventArgs _argsTo = new PropertyChangedEventArgs("To");
        private static readonly PropertyChangedEventArgs _argsFrom = new PropertyChangedEventArgs("From");

        /// <summary>
        /// Minimum value
        /// </summary>
        private T _from = default(T);
        /// <summary>
        /// Maximum value
        /// </summary>
        private T _to = default(T);

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        /// <summary>
        /// Initializes a new instance of the <see cref="RangeFilter&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="propertyInfo">The property info.</param>
        public RangeFilter(PropertyInfo propertyInfo)
            : base(propertyInfo)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="RangeFilter&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="propertyInfo">The property info.</param>
        /// <param name="from">From.</param>
        /// <param name="to">To.</param>
        public RangeFilter(PropertyInfo propertyInfo, T from, T to)
            : base(propertyInfo)
        {
            if (to == null)
            {
                throw new ArgumentNullException("to");
            }
            if (from == null)
            {
                throw new ArgumentNullException("from");
            }

            if (to.CompareTo(from) == -1)
            {
                throw new ArgumentException("Invalid input. <from> should be less or equal than <to>");
            }
            _to = to;
            _from = from;
        }

        /// <summary>
        /// Gets or sets the minimum value.
        /// </summary>
        /// <value>From.</value>
        public T From
        {
            get
            {
                return _from;
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("From");
                }
                if (_from.CompareTo(value) != 0)
                {
                    _from = value;
                    if (value.CompareTo(_to) > 0)
                    {
                        To = _from;
                    }
                    PropertyChanged(this, _argsFrom);
                    RaiseFilteringChanged();
                }
            }
        }

        /// <summary>
        /// Gets or sets he maximum value.
        /// </summary>
        /// <value>To.</value>
        public T To
        {
            get
            {
                return _to;
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("From");
                }
                if (_to.CompareTo(value) != 0)
                {
                    _to = value;
                    if (_to.CompareTo(_from) < 0)
                    {
                        From = _to;
                    }
                    PropertyChanged(this, _argsTo );
                    RaiseFilteringChanged();
                }
            }
        }


        /// <summary>
        /// Determines whether the specified target is a match.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <returns>
        /// 	<c>true</c> if the specified target is a match; otherwise, <c>false</c>.
        /// </returns>
        public override bool IsMatch(object target)
        {
            if (target == null)
            {
                return false;
            }

            T value = (T)PropertyInfo.GetValue(target, null);

            bool result = (value.CompareTo(_from) >= 0 && value.CompareTo(_to) <= 0);
            Debug.WriteLine(string.Format("Element:{0}", target));
            Debug.WriteLine(string.Format(" {0} ?<= {1} ?<= {2} : {3}", _from, value, _to, result));
            return result;
        }
    }
}

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

Share

About the Author

Stefan Bocutiu
Software Developer (Senior) Lab49
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150901.1 | Last Updated 29 Jan 2009
Article Copyright 2009 by Stefan Bocutiu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid