WPF customize DelegateCommand
2024-08-31 03:27:21
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows; namespace WpfApp55.ViewModel
{
public class VM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propName));
}
} private DelegateCommand UCCmdValue;
public DelegateCommand UCCmd
{
get
{
if(UCCmdValue==null)
{
UCCmdValue = new DelegateCommand(UCCmdExecuted, UCCmdCanExecute);
}
return UCCmdValue;
}
} private bool UCCmdCanExecute(object obj)
{
return true;
} private void UCCmdExecuted(object obj)
{
MessageBox.Show("You had clicked the customized button!");
}
}
}
public class DelegateCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute; public event EventHandler CanExecuteChanged; public DelegateCommand(Action<object> execute)
: this(execute, null)
{
} public DelegateCommand(Action<object> execute,
Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
} public bool CanExecute(object parameter)
{
if (_canExecute == null)
{
return true;
} return _canExecute(parameter);
} public void Execute(object parameter)
{
_execute(parameter);
} public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
最新文章
- Ubuntu下deb文件及tgz文件安装
- Windows 安装Kafka
- JS原生效果瀑布流布局的实现(一)
- [lintcode] Binary Tree Maximum Path Sum II
- haskell中的monad
- stardict
- C#Lambda表达式学习日记
- hdu5072(鞍山regional problem C):容斥,同色三角形模型
- delphi 文件夹权限设置(执行一个小脚本的笨办法)
- 分析Sizzle引擎
- js登录,回车登录
- 第十五节:深入理解async和await的作用及各种适用场景和用法
- lavarel 中间件
- 学习 MeteoInfo二次开发教程(一)
- sqlite读写
- POJ 2462 / HDU 1154 Cutting a Polygon
- iframe跨域与session失效问题
- 【C#】遍历List列表的同时,移除访问到的元素
- Vue学习笔记之Webpack介绍
- AE+C#实现:在SceneControl里打开和保存