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
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.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

Share

About the Author

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

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