<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>clsDelegateCommand</Title>
      <Shortcut>clsDC</Shortcut>
      <Description>DelegateCommand class</Description>
      <Author>Fons Sonnemans</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp">
          <![CDATA[public class DelegateCommand : DelegateCommand<object> {

        public DelegateCommand(Action executeAction) : base(o => executeAction()) {
        }

        public DelegateCommand(Action executeAction, Func<bool> canExecute)
            : base(o => executeAction(), o => canExecute()) {
        }
    }
          
          
          public class DelegateCommand<T> : ICommand {

        public event EventHandler CanExecuteChanged;

        private Predicate<T> _canExecute = null;
        private Action<T> _executeAction = null;

        public DelegateCommand(Action<T> executeAction) {
            _executeAction = executeAction;
        }

        public DelegateCommand(Action<T> executeAction, Predicate<T> canExecute)
            : this(executeAction) {
            _canExecute = canExecute;
        }

        public bool CanExecute(T parameter) {
            if (_canExecute != null) {
                return _canExecute(parameter);
            }
            return true;
        }

        public void Execute(T parameter) {
            if (_executeAction != null)
                _executeAction(parameter);

            if (CanExecuteChanged != null)
                CanExecuteChanged(this, EventArgs.Empty);
        }

        public void RaiseCanExecuteChanged() {
            OnCanExecuteChanged();
        }

        protected virtual void OnCanExecuteChanged() {
            EventHandler handler = CanExecuteChanged;
            if (handler != null) {
                handler(this, EventArgs.Empty);
            }
        }

        #region ICommand Members

        bool ICommand.CanExecute(object parameter) {
            return this.CanExecute((T)(parameter ?? default(T)));
        }

        void ICommand.Execute(object parameter) {
            this.Execute((T)parameter);
        }

        #endregion
    }

]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>