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;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using Stepi.Collections;

namespace Stepi.UIFilters
{
    public class FiltersViewPresentationModel
    {
        private PropertyInfo _propertyInfo;

        private bool _isDisplaying;

        private IFilteredCollection _filteredCollection;

        private IList<IFilterView> _views;

        private IUIFilterPresentationModel _previousActive = null;

        public FiltersViewPresentationModel(PropertyInfo propertyInfo, IFilteredCollection filteredCollection, IFilterViewInitializersManager initializer)
        {
            if (propertyInfo == null)
            {
                throw new ArgumentNullException("propertyInfo");
            }
            if (filteredCollection == null)
            {
                throw new ArgumentNullException("collection");
            }
            if (initializer == null)
            {
                throw new ArgumentNullException("initializer");
            }
            Debug.Assert(filteredCollection.GetType().IsGenericType, "Filtered collection needs to be a generic type");
            _propertyInfo = propertyInfo;
            _filteredCollection = filteredCollection;
            InitilizeViewsCollection(propertyInfo, initializer);
        }

        //public FiltersViewPresentationModel(string propertyName, IFilteredCollection filteredCollection, IFilterViewInitializersManager initializer)
        //{
        //    if (string.IsNullOrEmpty(propertyName))
        //    {
        //        throw new ArgumentException("Invalid property name specified", "propertyName");
        //    }
        //    if (initializer == null)
        //    {
        //        throw new ArgumentNullException("initializer");
        //    }
        //    if (filteredCollection == null)
        //    {
        //        throw new ArgumentNullException("filteredCollection");
        //    }
        //    if (!filteredCollection.GetType().IsGenericType)
        //    {
        //        throw new ArgumentException("Invalid filtered collection specified. Needs to be generic type");
        //    }
        //    Type[] genericTypes = filteredCollection.GetType().GetGenericArguments();
            
        //}

        private void InitilizeViewsCollection(PropertyInfo propertyInfo, IFilterViewInitializersManager initializer)
        {
            _views = new List<IFilterView>();
            foreach(IFilterView view in initializer.CreateView(propertyInfo, _filteredCollection))
            {
                //TODO: refactor this not to be dependend on the model
                Debug.Assert(view.Model != null, "Filter view presentation model is null");
                view.Model.PropertyChanged += new PropertyChangedEventHandler(OnFilterViewModelPropertyChanged);
                _views.Add(view);
            }
        }

        private void OnFilterViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            IUIFilterPresentationModel presentationModel = (IUIFilterPresentationModel)sender;
            if (presentationModel.IsActive)
            {
                if (presentationModel == _previousActive)
                {
                    return;
                }
                if (_previousActive != null)
                {
                    _previousActive.IsActive = false;
                    //no need for this one as it will raise the event and will get to the second else statement
                    //_filteredCollection.RemoveFilter(_previousActive.Filter);
                }
                _previousActive = presentationModel;
                _filteredCollection.AddFilter(presentationModel.Filter);
            }
            else
            {
                if (presentationModel == _previousActive)
                {
                    _previousActive = null;
                    _filteredCollection.RemoveFilter(presentationModel.Filter);
                }
            }
        }

        public IEnumerable<IFilterView> Filters
        {
            get 
            {
                foreach (IFilterView view in _views)
                {
                    yield return view;
                }
            }
        }

        public bool IsDisplaying
        {
            get
            {
                return _isDisplaying;
            }
            set
            {
                if (_isDisplaying != value)
                {
                    _isDisplaying = value;
                    foreach (IFilterView view in _views)
                    {
                        view.Model.UpdateData(value);
                    }
                }
            }
        }
    }
}

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.141223.1 | Last Updated 29 Jan 2009
Article Copyright 2009 by Stefan Bocutiu
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid