// Copyright © 2013 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Windows.Input;
namespace CefSharp.Wpf
{
///
/// DelegateCommand
///
///
internal class DelegateCommand : ICommand
{
///
/// The command handler
///
private readonly Action commandHandler;
///
/// The can execute handler
///
private readonly Func canExecuteHandler;
///
/// Occurs when changes occur that affect whether or not the command should execute.
///
public event EventHandler CanExecuteChanged;
///
/// Initializes a new instance of the class.
///
/// The command handler.
/// The can execute handler.
public DelegateCommand(Action commandHandler, Func canExecuteHandler = null)
{
this.commandHandler = commandHandler;
this.canExecuteHandler = canExecuteHandler;
}
///
/// Defines the method to be called when the command is invoked.
///
/// Data used by the command. If the command does not require data to be passed, this object can be set to null.
public void Execute(object parameter)
{
commandHandler();
}
///
/// Defines the method that determines whether the command can execute in its current state.
///
/// Data used by the command. If the command does not require data to be passed, this object can be set to null.
/// true if this command can be executed; otherwise, false.
public bool CanExecute(object parameter)
{
return
canExecuteHandler == null ||
canExecuteHandler();
}
///
/// Raises the can execute changed.
///
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
}