Click here to Skip to main content
15,891,431 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;
using System.Diagnostics;
using System.Windows.Threading;
using Stepi.Collections;

namespace Test.GridSorting
{
    class PresentationModel:INotifyPropertyChanged
    { 
        private const uint _itemsCount = 10;
        private ObservableCollection<Data> _data = new ObservableCollection<Data>();
        private ICollectionView _collectionView;

        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        private readonly DispatcherTimer _dataUpdateTimer;
        private readonly Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;
        private readonly Random _random = new Random((int)DateTime.Now.Ticks);

        private readonly int  _threadId;

        public PresentationModel()
        {
            _collectionView = new FilteredCollectionView<Data>(_data);
            InitializeData();

            _dataUpdateTimer = new DispatcherTimer();
            _dataUpdateTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
            _dataUpdateTimer.Tick += new EventHandler(OnUpdateTimerTick);

            _threadId = Thread.CurrentThread.ManagedThreadId;
        }

        private void OnUpdateTimerTick(object sender, EventArgs e)
        {
            Debug.Assert(_threadId == Thread.CurrentThread.ManagedThreadId, "Cross thread call");
            int index = _random.Next(0, (int)_itemsCount);
            if (!_dispatcher.CheckAccess())
            {
                _dispatcher.BeginInvoke(new ThreadStart(() =>
                {
                    _data[index].Value = _random.NextDouble() * 10000 / DateTime.Now.Millisecond;
                }), null);
                return;
            }
            _data[index].Value = _random.NextDouble() * 10000 / DateTime.Now.Millisecond;
        }

        public bool IsFakeUpdateEnabled
        {
            get { return _dataUpdateTimer.IsEnabled; }
            set
            {
                if (value)
                {
                    _dataUpdateTimer.Start();
                }
                else
                {
                    _dataUpdateTimer.Stop();
                }
            }
        }

        private void InitializeData()
        {
            for (uint i = 0; i < _itemsCount; i++)
            {
                _data.Add(new Data()
                {
                    FirstName = "First" + i,
                    LastName = "Last" + i,
                    Age = i,
                    Birthday = DateTime.Today.AddYears(-((int)i)),
                    
                });
            }
        }

        public ICollectionView Data
        {
            get { return _collectionView; }
        }


    }
}

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