Windows Presentation Foundation (WPF) controls have both an IsEnabled boolean property and a Click RoutedEventHandler which can be consolidated in the Command ICommand property. Commands combine the invoked execution and the logic that determines if the execution should run. In short, Commands combine both the User Interface (UI) logic with the execution logic.
For this example, we will use one of the more popular implementations of RelayCommand.
// RelayCommand.cs
public class RelayCommand<T> : ICommand
{
#region Fields
private readonly Action<T> _execute;
private readonly Predicate<T> _canExecute;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of <see><cref>DelegateCommand{T}</cref></see>.
/// </summary>
/// <param name="execute">
/// Delegate to execute when Execute is called on the command. This can be null to just hook up a
/// CanExecute delegate.
/// </param>
/// <remarks><seealso cref="CanExecute" /> will always return true.</remarks>
public RelayCommand(Action<T> execute)
: this(execute, null)
{
}
/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<T> execute, Predicate<T> canExecute)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
/// <summary>
/// Defines the method that determines whether the command can execute in its current state.
/// </summary>
/// <param name="parameter">
/// Data used by the command. If the command does not require data to be passed, this object can
/// be set to null.
/// </param>
/// <returns>
/// true if this command can be executed; otherwise, false.
/// </returns>
public bool CanExecute(object parameter)
{
return _canExecute?.Invoke((T) parameter) ?? true;
}
/// <summary>
/// Occurs when changes occur that affect whether or not the command should execute.
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
/// <summary>
/// Defines the method to be called when the command is invoked.
/// </summary>
/// <param name="parameter">
/// Data used by the command. If the command does not require data to be passed, this object can be
/// set to <see langword="null" />.
/// </param>
public void Execute(object parameter)
{
_execute((T) parameter);
}
#endregion
}
With many Model-View-ViewModel applications, it is typical to have a ViewModelBase as well.
// ViewModelBase.cs
public abstract class ViewModelBase : DependencyObject, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
After defining these two classes, the main ViewModel (in this example MainWindowViewModel) can be created.
// MainWindowViewModel.cs
public class MainWindowViewModel : ViewModelBase
{
public static string Process1Location = @"C:\Process1.exe";
public static string Process2Location = @"C:\process2.bat";
private RelayCommand<MainWindowViewModel> _runProcess1;
private RelayCommand<MainWindowViewModel> _runProcess2;
private static readonly Dictionary<string, Process> Processes
= new Dictionary<string, Process>();
public ICommand RunProcess1Command => _runProcess1 ??
(_runProcess1 =
new RelayCommand<MainWindowViewModel>(
param => RunProcess(Process1Location),
param => IsNotRunning(Process1Location)));
public ICommand RunProcess2Command => _runProcess2 ??
(_runProcess2 =
new RelayCommand<MainWindowViewModel>(
param => RunProcess(Process2Location),
param => IsNotRunning(Process2Location)));
/// <summary>
/// Runs the process based on its location.
/// </summary>
/// <param name="processLocation">The process location.</param>
/// <returns>The process</returns>
private static Process RunProcess(string processLocation)
{
if (!File.Exists(processLocation)) return null;
if (!IsNotRunning(processLocation)) return Processes[processLocation];
Processes[processLocation] = Process.Start(processLocation);
return Processes[processLocation];
}
/// <summary>
/// Determines if [the specified process name] [is not running].
/// </summary>
/// <param name="processLocation">The file that spooled up the process.</param>
/// <returns>True if the process is not running</returns>
private static bool IsNotRunning(string processLocation)
=> !Processes.ContainsKey(processLocation) || Processes[processLocation].HasExited;
}
Once the ViewModel is defined, the UserControl (MainWindow in this example) can instantiate it as its DataContext.
// MainWindow.xaml.cs
/// <summary>
/// The string for the ViewModel resource key
/// </summary>
public static string ViewModelKey = "ViewModelKey";
/// <summary>
/// Initializes a new instance of the <see cref="MainWindow" /> class.
/// </summary>
public MainWindow()
{
InitializeComponent();
Loaded += (sender, args) => { ViewModel = new MainWindowViewModel(); };
}
/// <summary>
/// Gets or sets the ViewModel.
/// </summary>
/// <value>
/// The ViewModel.
/// </value>
public MainWindowViewModel ViewModel
{
get { return (MainWindowViewModel) Resources[ViewModelKey]; }
set
{
if (value != null && Resources != null)
{
Resources[ViewModelKey] = value;
MainGrid.DataContext = value;
}
}
}
Finally, with the ViewModel in place, the Commands can be set in the XAML.
<!-- MainWindow.xaml -->
<Grid x:Name="MainGrid">
<StackPanel>
<Button Command="{Binding Path=RunProcess1Command}">Run Process 1</Button>
<Button Command="{Binding Path=RunProcess2Command}">Run Process 2</Button>
</StackPanel>
</Grid>
The resulting Control will allow the user to start the process, and then restart the process again after it has exited.