Files
Common-VS2019/SacsStandardBase/Base/Core/SacsCommand.cs
T
2021-05-05 15:36:28 +01:00

71 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
namespace Sarand.Net.Standard.Base.Core
{
//=======================================================================
/// <summary>
/// Code contract for commands - menu options, button actions, keyboard accelerators etc.
/// <para></para>
/// </summary>
public class SacsCommand : ICommand
{
//-------------------------------------------------------------------
// Definitions.
public delegate bool CanExecuteFn (object commandParam);
public delegate void ExecuteFn (object commandParam);
//-------------------------------------------------------------------
// Data members.
private readonly ExecuteFn executeFn;
private readonly CanExecuteFn canExecuteFn;
//-------------------------------------------------------------------
// Construction.
public SacsCommand (ExecuteFn executeFn, CanExecuteFn canExecuteFn = null)
{
this.executeFn = executeFn;
this.canExecuteFn = canExecuteFn;
}
//-------------------------------------------------------------------
// Properties.
public String CommandId { get; set; }
public String Caption { get; set; }
//-------------------------------------------------------------------
/// <summary>
/// CanExecuteChanged event handler.
/// <para>Unclear when this fires as Command won't know when state changes.</para>
/// </summary>
public event EventHandler CanExecuteChanged;
protected void FireCanExecuteChanged ()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
//-------------------------------------------------------------------
/// <summary>Determine whether this command can be executed.</summary>
public virtual bool CanExecute (object commandParam)
{
return canExecuteFn?.Invoke(commandParam) ?? false;
}
//-------------------------------------------------------------------
/// <summary>
/// Execute command.
/// <para>Only invokes command method if comand can be executed.</para>
/// </summary>
public virtual void Execute (object commandParam)
{
if (!CanExecute(commandParam)) return;
executeFn?.Invoke(commandParam);
}
//-------------------------------------------------------------------
}
//=======================================================================
}