原文:整理: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. Linux服务管理之DHCP

    1.DHCP服务简介 DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作, 主要有两个用途:给内部网络或网络 ...

  2. 安装docker后,导致qemu的桥接网络出现问题

    按照Qemu-4.1 桥接网络设置中介绍的方法建立起桥接网络后,可以实现虚拟机和host的相互ping,但是在虚拟机里去ping其他跟host处于同一个网段的ip地址时却失败了,然后ifconfig后 ...

  3. JAVA类与类之间的关系及代码示例

    参考链接:https://blog.csdn.net/wq6ylg08/article/details/81092056

  4. JSOI 2010 连通数

    洛谷 P4306 [JSOI2010]连通数 洛谷传送门 题目描述 度量一个有向图联通情况的一个指标是连通数,指图中可达顶点对个的个数. 如图 顶点 11 可达 1,~2,~3,~4,~51, 2, ...

  5. c# 关于反射

    反射的用途大体总结:1.使用Assembly定义和加载程序集,加载在程序集清单中列出模块,以及从程序集中查找类型并创建该类型的实例.CreateInstance2.使用Module了解包含模块的程序集 ...

  6. plv8 rpm包创建

    以下是从一个三方rpm构建,获取到的rpm 包制作spec,主要是学习下pg 扩展rpm 包的打包 rpm src 包 下载地址 https://fedora.pkgs.org/29/fedora-x ...

  7. three.js 添加三维坐标系

    //显示三维坐标系 ); scene.add(axis);

  8. 2-OpenResty 安装使用(Windows)

    下载 OpenResty 1.  https://gitee.com/yang456/LearnOpenResty.git 2.  http://openresty.org/cn/download.h ...

  9. ESA2GJK1DH1K基础篇: 测试APP使用SmartConfig绑定Wi-Fi 设备并控制设备

    前言 实现功能概要 STM32控制WI-Fi模块以AT指令TCP透传方式连接MQTT服务器, 实现MQTT通信控制. 测试准备工作(详细下载步骤请参考 硬件使用说明 ) 一,下载单片机程序 二,安装A ...

  10. 安装Visual Studio IntelliCode提供代码智能提示AI

    The Visual Studio IntelliCode extension provides AI-assisted development features for Python, TypeSc ...