Click here to Skip to main content
15,891,253 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 226.7K   8.4K   62  
Allows auto filtering functionality for DataGrid columns.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System;

namespace Stepi.UIFilters
{
    /// <summary>
    /// 
    /// </summary>
    [TemplatePart(Name=RangeFilterView.PART_FromInput, Type = typeof(TextBox))]
    [TemplatePart(Name = RangeFilterView.PART_ToInput, Type = typeof(TextBox))]
    public class RangeFilterView : FilterViewBase<IRangeFilterPresentationModel>
    {
        internal const string PART_FromInput = "PART_From";
        internal const string PART_ToInput = "PART_To";

        private TextBox _txtFrom;
        private TextBox _txtTo;

        /// <summary>
        /// Initializes a new instance of the <see cref="RangeFilterView"/> class.
        /// </summary>
        public RangeFilterView()
        {
            DefaultStyleKey = typeof(RangeFilterView);
        }

        /// <summary>
        /// Called when [apply template].
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            //if (_txtFrom != null)
            //{
            //    _txtFrom.BindingValidationError -= new System.EventHandler<ValidationErrorEventArgs>(OnBindingValidationError);
            //}
            _txtFrom = GetTemplateChild(PART_FromInput) as TextBox;

            //if (_txtTo != null)
            //{
            //    _txtTo.BindingValidationError += new System.EventHandler<ValidationErrorEventArgs>(OnBindingValidationError);
            //}
            _txtTo = GetTemplateChild(PART_ToInput) as TextBox;
            
            InitialiseBindings();
        }

        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        /// <value>The model.</value>
        public override IRangeFilterPresentationModel Model
        {
            get { return base.Model; }
            set
            {
                if (base.Model != value)
                {
                    base.Model = value;
                    InitialiseBindings();
                }
            }
        }

        /// <summary>
        /// Initialises the bindings.
        /// </summary>
        private void InitialiseBindings()
        {            
            IRangeFilterPresentationModel model = Model;
            if (model != null)
            {
                if (_txtFrom != null)
                {
                    _txtFrom.SetBinding(TextBox.TextProperty, CreateTextBoxBinding(model, "From"));
                    //_txtFrom.BindingValidationError += new System.EventHandler<ValidationErrorEventArgs>(OnBindingValidationError);
                }
                if (_txtTo != null)
                {
                    _txtTo.SetBinding(TextBox.TextProperty, CreateTextBoxBinding(model, "To"));
                    //_txtTo.BindingValidationError += new System.EventHandler<ValidationErrorEventArgs>(OnBindingValidationError);
                }
            }
        }

        /// <summary>
        /// Called when [binding validation error].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.ValidationErrorEventArgs"/> instance containing the event data.</param>
        [Obsolete]
        private void OnBindingValidationError(object sender, ValidationErrorEventArgs e)
        {
            IRangeFilterPresentationModel model = Model;
            if(model != null)
            {
                bool state = (e.Action == ValidationErrorEventAction.Removed);
                model.CanApply = state;
                if (!state)
                {
                    model.IsActive = state;
                }
            }
        }

        /// <summary>
        /// Creates the text box binding.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        private static Binding CreateTextBoxBinding(IRangeFilterPresentationModel model, string path)
        {
            Binding binding = new Binding();
            binding.Source = model;
            binding.ValidatesOnExceptions = true;
            binding.NotifyOnValidationError = true;
            binding.Mode = BindingMode.TwoWay;
            binding.Path = new PropertyPath(path);
            return binding;
        }
    }
}

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