WPF自定义命令

古城微笑少年丶 2021-09-10 22:32 459阅读 0赞

WPF自定义命令,以下实例为实现删除功能的命令。

1、创建DeleteCommand.cs类,并实现ICommand接口

命令实现了ICommand接口并继承了CanExecuteChanged事件、CanExecute方法和Execute方法。

  1. /// <summary>
  2. /// 删除命令
  3. /// </summary>
  4. public class DeleteCommand : ICommand
  5. {
  6. public event EventHandler CanExecuteChanged;
  7. public bool CanExecute(object parameter)
  8. {
  9. return true;
  10. }
  11. public void Execute(object parameter)
  12. {
  13. int Id = (int)parameter;
  14. if (Id <= 0)
  15. {
  16. return;
  17. }
  18. //执行删除方法
  19. }
  20. }

使用

  1. <Window.Resources>
  2. <!--引用资源-->
  3. <command:DeleteCommand x:Key="DeleteCommand"/>
  4. </Window.Resources>
  5. <Button Content="删除" Command="{StaticResource DeleteCommand}" CommandParameter="{Binding Id}"></Button>

发表评论

表情:
评论列表 (有 0 条评论,459人围观)

还没有评论,来说两句吧...

相关阅读

    相关 WPF 定义UI控件学习

      最近项目中运用到了WPF处理三维软件,在C/S结构中WPF做UI还是有很多优越性,简单的学了一点WPF知识,成功的完成项目目标。项目过度阶段对于WPF的一些基本特点有了进一