原文:整理:WPF中应用附加事件制作可以绑定命令的其他事件

目的:应用附加事件的方式定义可以绑定的事件,如MouseLeftButton、MouseDouble等等

一、定义属于Control的附加事件ControlAttachEvent类


  1. /// <summary> 附加事件 </summary>
  2. public static class ControlAttachEvent
  3. {
  4. #region - 双击事件 -
  5. public static readonly DependencyProperty PreviewMouseDoubleClickProperty =
  6. DependencyProperty.RegisterAttached("PreviewMouseDoubleClick", typeof(ICommand), typeof(ControlAttachEvent), new FrameworkPropertyMetadata(OnCommandChanged));
  7. public static ICommand GetPreviewMouseDoubleClick(Control target)
  8. {
  9. return (ICommand)target.GetValue(PreviewMouseDoubleClickProperty);
  10. }
  11. public static void SetPreviewMouseDoubleClick(Control target, ICommand value)
  12. {
  13. target.SetValue(PreviewMouseDoubleClickProperty, value);
  14. }
  15. private static void Element_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  16. {
  17. Control control = sender as Control;
  18. ICommand command = GetPreviewMouseDoubleClick(control);
  19. if (command.CanExecute(sender))
  20. {
  21. command.Execute(sender);
  22. e.Handled = true;
  23. }
  24. }
  25. private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  26. {
  27. Control control = d as Control;
  28. control.PreviewMouseDoubleClick += new MouseButtonEventHandler(Element_PreviewMouseDoubleClick);
  29. }
  30. #endregion
  31. public static DependencyProperty PreviewMouseLeftButtonDownCommandProperty = DependencyProperty.RegisterAttached("PreviewMouseLeftButtonDown",typeof(ICommand),typeof(ControlAttachEvent),new FrameworkPropertyMetadata(null, new PropertyChangedCallback(PreviewMouseLeftButtonDownChanged)));
  32. public static void SetPreviewMouseLeftButtonDown(DependencyObject target, ICommand value)
  33. {
  34. target.SetValue(PreviewMouseLeftButtonDownCommandProperty, value);
  35. }
  36. public static ICommand GetPreviewMouseLeftButtonDown(DependencyObject target)
  37. {
  38. return (ICommand)target.GetValue(PreviewMouseLeftButtonDownCommandProperty);
  39. }
  40. private static void PreviewMouseLeftButtonDownChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
  41. {
  42. FrameworkElement element = target as FrameworkElement;
  43. if (element != null)
  44. {
  45. if ((e.NewValue != null) && (e.OldValue == null))
  46. {
  47. element.PreviewMouseLeftButtonDown += element_PreviewMouseLeftButtonDown;
  48. }
  49. else if ((e.NewValue == null) && (e.OldValue != null))
  50. {
  51. element.PreviewMouseLeftButtonDown -= element_PreviewMouseLeftButtonDown;
  52. }
  53. }
  54. }
  55. private static void element_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  56. {
  57. FrameworkElement element = (FrameworkElement)sender;
  58. ICommand command = (ICommand)element.GetValue(PreviewMouseLeftButtonDownCommandProperty);
  59. command.Execute(sender);
  60. }
  61. }

说明:当控件MyControl中应用该项附加事件,如注册PreviewMouseDoubleClick事件,则会触发更新方法OnCommandChanged,而在更新方法中则会注册该控件MyControl的双击事件绑定的命令,由此实现双击该控件触发绑定命令的功能;

二、调用方法

  <Button  base:ControlAttachEvent.PreviewMouseDoubleClick="{Binding RelayCommand}"/>

如上代码当双击Button时会触发ViewModel中绑定的RelayCommand命令

注:

优点在于可以把控件中不支持绑定的事件支持绑定,同时应用附加属性复用性更强

缺点在于传递事件的参数只有sender,agrs需要特殊处理,同时支持的事件与Control有关,其他更上层的控件需要单独定义

最新文章

  1. MVC4做网站后台:用户管理 ——用户组
  2. 封装WebAPI客户端,附赠Nuget打包上传VS拓展工具
  3. HBase之创建表
  4. 通过chrome 获取网站的cookie信息
  5. MYSQL学习笔记3--mysql 2PC二阶段协义 与 日志闪回
  6. UVaLive4043 UVa1411 Ants 巨人与鬼
  7. 初次接触GWT,知识点总括
  8. IO流(File类
  9. map对象建立家族姓氏查询
  10. oracle_常用命令(表空间查询)
  11. C#排列组合类
  12. HTML添加样式三种办法
  13. 【ASP.NET MVC 学习笔记】- 05 依赖注入工具Ninject
  14. 在servlet中使用@Autowired注解无法注入实例的问题
  15. Octave 命令积累
  16. php代码审计之变量覆盖
  17. Spark:聚类算法之LDA主题模型算法
  18. springboot2.0配置连接池(hikari、druid)
  19. 二、Laravel手动下载安装及初始化配置(此处以Laravel5.2为例)
  20. Unix环境高级编程:fork, vfork, clone

热门文章

  1. mysql官网下载对应的mysql包
  2. Linux Shell脚本编程while语句
  3. spring boot缓存excel临时文件后再操作
  4. LAMP架构性能测试+php优化
  5. 注意力机制---Attention、local Attention、self Attention、Hierarchical attention
  6. selenium 加载出新的窗口
  7. sessionId控制单点登陆
  8. Centos7安装MySQL(多图)
  9. three.js 居中-组
  10. 【AGC009E】Eternal Average