原文:整理: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有关,其他更上层的控件需要单独定义

整理:WPF中应用附加事件制作可以绑定命令的其他事件的更多相关文章

  1. 转:WPF中ListBox的创建和多种绑定用法

    先从最容易的开始演示ListBox控件的创建. Adding ListBox Items下面的代码是向ListBox控件中添加多项ListBoxItem集合.XAML代码如下:<ListBox ...

  2. 在WPF中一种较好的绑定Enums数据方法

    引言 在你使用wpf应用程序开发的时候,是否需要进行数据绑定到Enum数据呢?在这篇文章中,我将向你展示在WPF中处理Enum数据绑定的方法. 假设存在一个这样的Enum数据的定义,具体内容如下文代码 ...

  3. jsp中一个标签两种方式绑定两个click事件导致未执行的问题

    近日,在开发过程中,写了一个标签 <li id="a1" onclick="doSomething()">...</li> 在js页面中 ...

  4. WPF中,Combox的SelectedItem属性绑定成功后,未能默认显示上一次选择的结果。

    问题描述: Combox中,设定了绑定对象,但是在第一次进入时却没有显示上次选中的项.      1)查看SelectedItem对应绑定的值,也是有的(启动时,读取上次设置的结果,来初始化界面). ...

  5. WPF中DataGrid的ComboBox的简单绑定方式(绝对简单)

    在写次文前先不得不说下网上的其他wpf的DataGrid绑定ComboBox的方式,看了之后真是让人欲仙欲死. 首先告诉你一大堆的模型,一大堆的控件模板,其实或许你紧紧只想知道怎么让combobox怎 ...

  6. WPF中使用相对资源来进行绑定,数据源是通过DataContext来指定的

    1. 最外层是Window是对象,Window的ItemsControl使用了ItemsTemplate,然后在ItemsTemplate中要绑定Language属性, 而整个Window的数据源是通 ...

  7. 01.WPF中制作无边框窗体

    [引用:]http://blog.csdn.net/johnsuna/article/details/1893319   众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormB ...

  8. WPF中制作无边框窗体

    原文:WPF中制作无边框窗体 众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormBorderStyle属性设置为None来完成.如果要制作成异形窗体,则需要使用图片或者使用G ...

  9. 整理:WPF中Binding的几种写法

    原文:整理:WPF中Binding的几种写法 目的:整理WPF中Bind的写法 <!--绑定到DataContext--> <Button Content="{Bindin ...

随机推荐

  1. vscode vue 去掉语法提示

    在vscode中,点击file->preferences->settings, 然后输入vetur, 滚到最下面,那个勾去掉,然后关闭,重启vscode就可以了

  2. 4.redis 的过期策略都有哪些?内存淘汰机制都有哪些?手写一下 LRU 代码实现?

    作者:中华石杉 面试题 redis 的过期策略都有哪些?内存淘汰机制都有哪些?手写一下 LRU 代码实现? 面试官心理分析 如果你连这个问题都不知道,上来就懵了,回答不出来,那线上你写代码的时候,想当 ...

  3. itextpdf中表格中单元格的文字水平垂直居中的设置

    在使用itextpdf中,版本是5.5.6,使用Doucument方式生成pdf时,设置单元格中字体的对齐方式时,发现一些问题,并逐渐找到了解决方式. 给我的经验就是:看官网的例子才能保证代码的效果, ...

  4. Golang 需要避免踩的 50 个坑1

    最近准备写一些关于golang的技术博文,本文是之前在GitHub上看到的golang技术译文,感觉很有帮助,先给各位读者分享一下. 前言 Go 是一门简单有趣的编程语言,与其他语言一样,在使用时不免 ...

  5. 配置以https访问网站

    环境 centos7  nginx1.16.1 一.申请证书(已有域名) 进入阿里云控制台,点击域名(我已经弄好了,一开始是没有ssl选项) 点击免费开启ssl 点购买->选择免费版 购买成功后 ...

  6. ZABBIX监控TCP连接状态

    一.获取监控数据 # /bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}' LISTEN ESTABLISHED T ...

  7. V2X:

    一篇文章读懂V2X系列:标准篇 为了提升交通系统的安全性和智能化,智能交通系统的概念正逐渐兴起.智能交通可以利用新一代的通信网络和数据处理能力,提高现有交通系统的整体效率,降低能量损耗,增加运输的安全 ...

  8. USACO Party Invitations

    洛谷 P3068 [USACO13JAN]派对邀请函Party Invitations 洛谷传送门 JDOJ 2343: USACO 2013 Jan Silver 3.Party Invitatio ...

  9. ESA2GJK1DH1K基础篇: STM32+GPRS(AT指令版)实现MQTT源码讲解(支持Air202,SIM800)

    前言 注: 本程序发送心跳包,发送温湿度,返回控制数据这三个发送是单独的,有可能凑到一起发. 由于本身程序就是复杂性的程序,所以这节程序没有使用中断发送,没有使用环形队列发送,为了避免多条消息可能凑到 ...

  10. 待办事项App 评测

    1. 敬业签 2. Microsoft To-Do(奇妙清单) 3. Evernote 4.one note 5.Google Keep 6.to-do-ist 7.365 日历 8.Any.Do 9 ...