|
// --------------------------------------------------------------------
// Member of the WPFSmartLibrary
// For more information see : http://wpfsmartlibrary.codeplex.com/
// (by DotNetMastermind)
//
// filename : ActionCommand.cs
// namespace : SoftArcs.WPFSmartLibrary.MVVMCommands
// class(es) : ActionCommand
//
// --------------------------------------------------------------------
using System;
using System.Windows.Input;
namespace SoftArcs.WPFSmartLibrary.MVVMCommands
{
/// <summary>
/// This class is similar to the "ActionCommand"-class from the 'Microsoft.Expression.Interactivity.Core'-namespace
/// which is included in the 'Microsoft.Expression.Interactions.dll' from the Microsoft Blend 4 SDK
/// (so you do not need to install the SDK to use this command)
/// </summary>
public class ActionCommand : ICommand
{
private readonly Action executeHandler;
private readonly Action<object> executeHandlerWithParameter;
private readonly Func<object, bool> canExecuteHandler;
/// <summary>
/// Parameterless version of the ActionCommand
/// </summary>
/// <param name="execute"></param>
public ActionCommand(Action execute)
{
if (execute == null)
{
throw new ArgumentNullException( "Execute can't be null", new Exception() );
}
this.executeHandler = execute;
}
/// <summary>
/// ActionCommand with an object-parameter
/// </summary>
/// <param name="execute"></param>
public ActionCommand(Action<object> execute)
{
if (execute == null)
{
throw new ArgumentNullException( "Execute can't be null", new Exception() );
}
this.executeHandlerWithParameter = execute;
}
/// <summary>
/// ActionCommand with an object-parameter and a CanExecute delegate
/// </summary>
/// <param name="execute"></param>
/// <param name="canExecute"> </param>
public ActionCommand(Action<object> execute, Func<object, bool> canExecute)
: this( execute )
{
this.canExecuteHandler = canExecute;
}
#region Members of the 'ICommand' interface
public void Execute(object args)
{
if (this.executeHandlerWithParameter != null)
{
this.executeHandlerWithParameter( args );
}
else
{
this.executeHandler();
}
}
public bool CanExecute(object args)
{
if (this.canExecuteHandler == null)
{
return true;
}
return this.canExecuteHandler( args );
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
#endregion // Members of the 'ICommand' interface
}
}
|
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.
Experience:
More than 20 years with software design, architecture and development. More than 9 years with the .NET Framework.
Preferences:
C#, .NET 2.0, .NET 3.5, .NET 4.0, .NET 4.5, WPF (3, 4 and 4.5), MVVM, XAML, Silverlight, Windows Phone, Windows 8 Apps, ASP.NET, WCF, T-SQL and especially GUI-Development and GUI-Design.
I would say I am a dotNet Developer with a keen affinity for developing and enriching user interfaces.