104 lines
3.3 KiB
C#
104 lines
3.3 KiB
C#
// --------------------------------------------------------------------------------------------------------------------
|
|
// <copyright file="DelegateCommand.cs" company="OxyPlot">
|
|
// Copyright (c) 2014 OxyPlot contributors
|
|
// </copyright>
|
|
// <summary>
|
|
// Represents a delegate command.
|
|
// </summary>
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
|
|
namespace WpfExamples
|
|
{
|
|
using System;
|
|
using System.Windows.Input;
|
|
|
|
/// <summary>
|
|
/// Represents a delegate command.
|
|
/// </summary>
|
|
public class DelegateCommand : ICommand
|
|
{
|
|
/// <summary>
|
|
/// The can execute.
|
|
/// </summary>
|
|
private readonly Func<bool> canExecute;
|
|
|
|
/// <summary>
|
|
/// The execute.
|
|
/// </summary>
|
|
private readonly Action execute;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DelegateCommand" /> class.
|
|
/// </summary>
|
|
/// <param name="execute">The execute.</param>
|
|
public DelegateCommand(Action execute)
|
|
: this(execute, null)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DelegateCommand" /> class.
|
|
/// </summary>
|
|
/// <param name="execute">The execute.</param>
|
|
/// <param name="canExecute">The can execute.</param>
|
|
public DelegateCommand(Action execute, Func<bool> canExecute)
|
|
{
|
|
if (execute == null)
|
|
{
|
|
throw new ArgumentNullException("execute");
|
|
}
|
|
|
|
this.execute = execute;
|
|
this.canExecute = canExecute;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Occurs when changes occur that affect whether or not the command should execute.
|
|
/// </summary>
|
|
public event EventHandler CanExecuteChanged
|
|
{
|
|
add
|
|
{
|
|
if (this.canExecute != null)
|
|
{
|
|
CommandManager.RequerySuggested += value;
|
|
}
|
|
}
|
|
|
|
remove
|
|
{
|
|
if (this.canExecute != null)
|
|
{
|
|
CommandManager.RequerySuggested -= value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <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 <c>null</c>.</param>
|
|
/// <returns><c>true</c> if this command can be executed; otherwise, <c>false</c>.</returns>
|
|
public bool CanExecute(object parameter)
|
|
{
|
|
return this.canExecute == null ? true : this.canExecute();
|
|
}
|
|
|
|
/// <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 <c>null</c>.</param>
|
|
public void Execute(object parameter)
|
|
{
|
|
this.execute();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises the can execute changed.
|
|
/// </summary>
|
|
public void RaiseCanExecuteChanged()
|
|
{
|
|
CommandManager.InvalidateRequerySuggested();
|
|
}
|
|
}
|
|
} |