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;

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

Share

About the Author

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

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