// Copyright © 2017 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.Example { public class RelayCommand : ICommand { private readonly Action commandHandler; private readonly Func canExecuteHandler; public event EventHandler CanExecuteChanged; public RelayCommand(Action commandHandler, Func canExecuteHandler = null) { this.commandHandler = commandHandler; this.canExecuteHandler = canExecuteHandler; } public RelayCommand(Action commandHandler, Func canExecuteHandler = null) : this(_ => commandHandler(), canExecuteHandler == null ? null : new Func(_ => canExecuteHandler())) { } public void Execute(object parameter) { commandHandler(parameter); } public bool CanExecute(object parameter) { return canExecuteHandler == null || canExecuteHandler(parameter); } public void RaiseCanExecuteChanged() { if (CanExecuteChanged != null) { CanExecuteChanged(this, EventArgs.Empty); } } } public class RelayCommand : RelayCommand { public RelayCommand(Action commandHandler, Func canExecuteHandler = null) : base(o => commandHandler(o is T t ? t : default(T)), canExecuteHandler == null ? null : new Func(o => canExecuteHandler(o is T t ? t : default(T)))) { } } }