downkyi/DownKyi/CustomControl/MyDelegateCommand.cs
2022-02-26 22:23:59 +08:00

86 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Windows.Input;
namespace DownKyi.CustomControl
{
/// <summary>
/// 绑定命令的工具类
/// </summary>
public class MyDelegateCommand : ICommand
{
/// <summary>
/// 检查命令是否可以执行的事件在UI事件发生导致控件状态或数据发生变化时触发
/// </summary>
public event EventHandler CanExecuteChanged
{
add
{
if (_canExecute != null)
{
CommandManager.RequerySuggested += value;
}
}
remove
{
if (_canExecute != null)
{
CommandManager.RequerySuggested -= value;
}
}
}
/// <summary>
/// 判断命令是否可以执行的方法
/// </summary>
private Func<object, bool> _canExecute;
/// <summary>
/// 命令需要执行的方法
/// </summary>
private Action<object> _execute;
/// <summary>
/// 创建一个命令
/// </summary>
/// <param name="execute">命令要执行的方法</param>
public MyDelegateCommand(Action<object> execute) : this(execute, null)
{
}
/// <summary>
/// 创建一个命令
/// </summary>
/// <param name="execute">命令要执行的方法</param>
/// <param name="canExecute">判断命令是否能够执行的方法</param>
public MyDelegateCommand(Action<object> execute, Func<object, bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
/// <summary>
/// 判断命令是否可以执行
/// </summary>
/// <param name="parameter">命令传入的参数</param>
/// <returns>是否可以执行</returns>
public bool CanExecute(object parameter)
{
if (_canExecute == null) return true;
return _canExecute(parameter);
}
/// <summary>
/// 执行命令
/// </summary>
/// <param name="parameter"></param>
public void Execute(object parameter)
{
if (_execute != null && CanExecute(parameter))
{
_execute(parameter);
}
}
}
}