Click here to Skip to main content
15,892,298 members
Articles / Desktop Programming / WPF

Implement a Firefox-like search in WPF applications using M-V-VM

Rate me:
Please Sign up or sign in to vote.
4.91/5 (27 votes)
1 Feb 2009CPOL5 min read 95.9K   1.6K   69  
How to add a Firefox-like incremental search to WPF applications using the M-V-VM pattern. The search is performed directly on a CollectionView, so it can be used with any WPF items control.
using System;
using System.Windows.Input;

namespace CodeMind.FirefoxLikeSearch.Infrastructure
{
    /// <summary>
    /// Represents an <see cref="ICommand"/> which runs an event handler when it is invoked.
    /// </summary>
    public class DelegateCommand : ICommand
    {
        private readonly Action<object> _executeAction;
        private readonly Predicate<object> _canExecute;

        /// <summary>
        /// Raised when changes occur that affect whether or not the command should execute.
        /// </summary>
        /// <remarks>
        /// The trick to integrate into WPF command manager found on: 
        /// http://joshsmithonwpf.wordpress.com/2008/06/17/allowing-commandmanager-to-query-your-icommand-objects/
        /// </remarks>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>
        /// Creates a new instance of <see cref="DelegateCommand"/> and assigns the given action to it.
        /// </summary>
        /// <param name="executeAction">Event handler to assign to the command.</param>
        public DelegateCommand(Action<object> executeAction) : this(executeAction, null)
        {
        }

        /// <summary>
        /// Creates a new instance of <see cref="DelegateCommand"/> and assigns the given action and predicate to it.
        /// </summary>
        /// <param name="executeAction">Event handler to assign to the command.</param>
        /// <param name="canExecute">Predicate to check whether the command can be executed.</param>
        public DelegateCommand(Action<object> executeAction, Predicate<object> canExecute)
        {
            _executeAction = executeAction;
            _canExecute = canExecute;
        }

        /// <summary>
        /// Defines the method that determines whether the command can execute in its current state.
        /// </summary>
        /// <returns>
        /// true if this command can be executed; otherwise, false.
        /// </returns>
        /// <param name="parameter">Data used by the command. If the command does not require data 
        /// to be passed, this object can be set to null.</param>
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute.Invoke(parameter);
        }

        /// <summary>
        /// Defines the method to be called when the command is invoked. The method will invoke the
        /// attached event handler.
        /// </summary>
        /// <param name="parameter">Data used by the command. If the command does not require data 
        /// to be passed, this object can be set to null.</param>
        public void Execute(object parameter)
        {
            _executeAction.Invoke(parameter);
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Architect Abacus Solutions and Consulting, Inc.
Bosnia and Herzegovina Bosnia and Herzegovina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions