Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Auto-filter for Microsoft WPF DataGrid

, 29 Jan 2009 Eclipse
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.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System;

namespace Stepi.UIFilters
{
    [TemplatePart(Name=CompareFilterView.PART_Input, Type=typeof(TextBox))]
    public class CompareFilterView : FilterViewBase<ICompareFilterPresentationModel>
    {
        internal const string PART_Input = "PART_Input";

        private TextBox _txtInput;

        public CompareFilterView()
        {
            DefaultStyleKey = typeof(CompareFilterView);
        }

        public override ICompareFilterPresentationModel Model
        {
            get { return base.Model; }
            set
            {
                if (base.Model != value)
                {
                    ClearBindings();
                    base.Model = value;
                    InitializeBindings();
                }
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            //if (_txtInput != null)
            //{
            //    _txtInput.BindingValidationError -= new System.EventHandler<ValidationErrorEventArgs>(OnBindingValidationError);
            //}
            _txtInput = GetTemplateChild(PART_Input) as TextBox;
            InitializeBindings();
        }

        private void ClearBindings()
        {
            if (_txtInput != null)
            {
                //can not have null as binding.
                _txtInput.SetBinding(TextBox.TextProperty, new Binding());
            }
        }

        private void InitializeBindings()
        {
            ICompareFilterPresentationModel model = Model;
            if (model != null && _txtInput != null)
            {
                Binding binding = CreateBinding(model);
                _txtInput.SetBinding(TextBox.TextProperty, binding);
                //_txtInput.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)
        {
            ICompareFilterPresentationModel model = Model;
            if (model != null)
            {
                bool state = (e.Action == ValidationErrorEventAction.Removed);
                model.CanApply = state;
                if (!state)
                {
                    model.IsActive = state;
                }
            }
        }

        /// <summary>
        /// Creates the binding.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        private static Binding CreateBinding(ICompareFilterPresentationModel model)
        {
            Binding binding = new Binding();
            binding.NotifyOnValidationError = true;
            binding.ValidatesOnExceptions = true;
            binding.Path = new PropertyPath("Target");
            binding.Source = model;
            binding.Mode = BindingMode.TwoWay;
            binding.ValidationRules.Add(new DataErrorValidationRule());
            binding.ValidationRules.Add(new ExceptionValidationRule());
            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

Share

About the Author

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

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