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

namespace Stepi.UIFilters
{
    [TemplatePart(Name = FiltersView.PART_DropDown, Type = typeof(DropDownButton))]
    public class FiltersView : Control
    {
        internal const string PART_DropDown = "PART_DropDown";

        private DropDownButton _filterViewItems = null;

        static FiltersView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FiltersView), new FrameworkPropertyMetadata(typeof(FiltersView)));
        }

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

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _filterViewItems = GetTemplateChild(PART_DropDown) as DropDownButton;
            if (_filterViewItems != null)
            {
                //ComboBox cb = _filterViewItems as ComboBox;
                //if (cb != null)
                //{
                //    cb.SelectionChanged += new SelectionChangedEventHandler(OnSelectionChanged);
                //}
                _filterViewItems.GotFocus += new RoutedEventHandler(OnGotFocus);
                _filterViewItems.LostFocus += new RoutedEventHandler(OnLostFocus);
                //_filterViewItems.DropDown = new ContextMenu();
                //check the Model
                FiltersViewPresentationModel model = Model;
                if (model != null && _filterViewItems.DropDown != null)
                {
                    _filterViewItems.DropDown.ItemsSource = model.Filters;
                }
            }
        }

        //void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    ComboBox cb = sender as ComboBox;
        //    cb.SelectedIndex = -1;
        //}

        void OnLostFocus(object sender, RoutedEventArgs e)
        {
            FiltersViewPresentationModel model = Model;
            if(model != null)
            {
               model.IsDisplaying = false;
            }
        }

        void OnGotFocus(object sender, RoutedEventArgs e)
        {
            FiltersViewPresentationModel model = Model;
            if (model != null)
            {
                model.IsDisplaying = true;
            }
        }

        public FiltersViewPresentationModel Model
        {
            get { return DataContext as FiltersViewPresentationModel; }
            set
            {
                if (DataContext != value)
                {
                    DataContext = value;

                    if (_filterViewItems != null)
                    {
                        _filterViewItems.DropDown.ItemsSource = value != null ? value.Filters : null;
                    }
                }
            }
        }
    }
}

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