ICommand


Snippet Details

Shortcut
icommand
Description
Code snippet for ICommand Members
Language
csharp
Author
Koen Hendriks
Upload on
4/24/2014 7:49:43 PM
Downloads
854

Code Output

        #region ICommand Members

        public class DelegateCommand : ICommand
        {
            Func<object, bool> canExecute;
            Action<object> executeAction;

            public DelegateCommand(Action<object> executeAction)
                : this(executeAction, null)
            {
            }

            public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
            {
                if (executeAction == null)
                {
                    throw new ArgumentNullException("executeAction");
                }
                this.executeAction = executeAction;
                this.canExecute = canExecute;
            }

            public bool CanExecute(object parameter)
            {
                bool result = true;
                Func<object, bool> canExecuteHandler = this.canExecute;
                if (canExecuteHandler != null)
                {
                    result = canExecuteHandler(parameter);
                }

                return result;
            }

            public event EventHandler CanExecuteChanged;

            public void RaiseCanExecuteChanged()
            {
                EventHandler handler = this.CanExecuteChanged;
                if (handler != null)
                {
                    handler(this, new EventArgs());
                }
            }

            public void Execute(object parameter)
            {
                this.executeAction(parameter);
            }
        }

        #endregion
    

Download Add to .VSIX Package


comments powered by Disqus

Extension Package

No snippets are added


Languages