mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-04 10:22:55 +00:00
56 lines
1.7 KiB
C#
56 lines
1.7 KiB
C#
// 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<object> commandHandler;
|
|
private readonly Func<object, bool> canExecuteHandler;
|
|
|
|
public event EventHandler CanExecuteChanged;
|
|
|
|
public RelayCommand(Action<object> commandHandler, Func<object, bool> canExecuteHandler = null)
|
|
{
|
|
this.commandHandler = commandHandler;
|
|
this.canExecuteHandler = canExecuteHandler;
|
|
}
|
|
public RelayCommand(Action commandHandler, Func<bool> canExecuteHandler = null)
|
|
: this(_ => commandHandler(), canExecuteHandler == null ? null : new Func<object, bool>(_ => 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<T> : RelayCommand
|
|
{
|
|
public RelayCommand(Action<T> commandHandler, Func<T, bool> canExecuteHandler = null)
|
|
: base(o => commandHandler(o is T t ? t : default(T)), canExecuteHandler == null ? null : new Func<object, bool>(o => canExecuteHandler(o is T t ? t : default(T))))
|
|
{
|
|
}
|
|
}
|
|
}
|