ICommand


Snippet Details

Shortcut
icommand
Description
Code snippet for ICommand Members
Language
csharp
Author
Koen Hendriks
Upload on
24-4-2014 19:49:43
Downloads
2491

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