整理:WPF中应用附加事件制作可以绑定命令的其他事件
目的:应用附加事件的方式定义可以绑定的事件,如MouseLeftButton、MouseDouble等等
一、定义属于Control的附加事件ControlAttachEvent类
-
/// <summary> 附加事件 </summary>
-
public static class ControlAttachEvent
-
{
-
-
#region - 双击事件 -
-
-
public static readonly DependencyProperty PreviewMouseDoubleClickProperty =
-
DependencyProperty.RegisterAttached("PreviewMouseDoubleClick", typeof(ICommand), typeof(ControlAttachEvent), new FrameworkPropertyMetadata(OnCommandChanged));
-
-
public static ICommand GetPreviewMouseDoubleClick(Control target)
-
{
-
return (ICommand)target.GetValue(PreviewMouseDoubleClickProperty);
-
}
-
-
public static void SetPreviewMouseDoubleClick(Control target, ICommand value)
-
{
-
target.SetValue(PreviewMouseDoubleClickProperty, value);
-
}
-
-
private static void Element_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
-
{
-
Control control = sender as Control;
-
-
ICommand command = GetPreviewMouseDoubleClick(control);
-
-
if (command.CanExecute(sender))
-
{
-
command.Execute(sender);
-
e.Handled = true;
-
}
-
}
-
-
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
-
{
-
Control control = d as Control;
-
-
control.PreviewMouseDoubleClick += new MouseButtonEventHandler(Element_PreviewMouseDoubleClick);
-
}
-
#endregion
-
-
public static DependencyProperty PreviewMouseLeftButtonDownCommandProperty = DependencyProperty.RegisterAttached("PreviewMouseLeftButtonDown",typeof(ICommand),typeof(ControlAttachEvent),new FrameworkPropertyMetadata(null, new PropertyChangedCallback(PreviewMouseLeftButtonDownChanged)));
-
-
public static void SetPreviewMouseLeftButtonDown(DependencyObject target, ICommand value)
-
{
-
target.SetValue(PreviewMouseLeftButtonDownCommandProperty, value);
-
}
-
-
public static ICommand GetPreviewMouseLeftButtonDown(DependencyObject target)
-
{
-
return (ICommand)target.GetValue(PreviewMouseLeftButtonDownCommandProperty);
-
}
-
-
private static void PreviewMouseLeftButtonDownChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
-
{
-
FrameworkElement element = target as FrameworkElement;
-
if (element != null)
-
{
-
if ((e.NewValue != null) && (e.OldValue == null))
-
{
-
element.PreviewMouseLeftButtonDown += element_PreviewMouseLeftButtonDown;
-
}
-
else if ((e.NewValue == null) && (e.OldValue != null))
-
{
-
element.PreviewMouseLeftButtonDown -= element_PreviewMouseLeftButtonDown;
-
}
-
}
-
}
-
-
private static void element_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
-
{
-
FrameworkElement element = (FrameworkElement)sender;
-
ICommand command = (ICommand)element.GetValue(PreviewMouseLeftButtonDownCommandProperty);
-
command.Execute(sender);
-
}
-
}
说明:当控件MyControl中应用该项附加事件,如注册PreviewMouseDoubleClick事件,则会触发更新方法OnCommandChanged,而在更新方法中则会注册该控件MyControl的双击事件绑定的命令,由此实现双击该控件触发绑定命令的功能;
二、调用方法
<Button base:ControlAttachEvent.PreviewMouseDoubleClick="{Binding RelayCommand}"/>
如上代码当双击Button时会触发ViewModel中绑定的RelayCommand命令
注:
优点在于可以把控件中不支持绑定的事件支持绑定,同时应用附加属性复用性更强
缺点在于传递事件的参数只有sender,agrs需要特殊处理,同时支持的事件与Control有关,其他更上层的控件需要单独定义
整理:WPF中应用附加事件制作可以绑定命令的其他事件的更多相关文章
- 转:WPF中ListBox的创建和多种绑定用法
先从最容易的开始演示ListBox控件的创建. Adding ListBox Items下面的代码是向ListBox控件中添加多项ListBoxItem集合.XAML代码如下:<ListBox ...
- 在WPF中一种较好的绑定Enums数据方法
引言 在你使用wpf应用程序开发的时候,是否需要进行数据绑定到Enum数据呢?在这篇文章中,我将向你展示在WPF中处理Enum数据绑定的方法. 假设存在一个这样的Enum数据的定义,具体内容如下文代码 ...
- jsp中一个标签两种方式绑定两个click事件导致未执行的问题
近日,在开发过程中,写了一个标签 <li id="a1" onclick="doSomething()">...</li> 在js页面中 ...
- WPF中,Combox的SelectedItem属性绑定成功后,未能默认显示上一次选择的结果。
问题描述: Combox中,设定了绑定对象,但是在第一次进入时却没有显示上次选中的项. 1)查看SelectedItem对应绑定的值,也是有的(启动时,读取上次设置的结果,来初始化界面). ...
- WPF中DataGrid的ComboBox的简单绑定方式(绝对简单)
在写次文前先不得不说下网上的其他wpf的DataGrid绑定ComboBox的方式,看了之后真是让人欲仙欲死. 首先告诉你一大堆的模型,一大堆的控件模板,其实或许你紧紧只想知道怎么让combobox怎 ...
- WPF中使用相对资源来进行绑定,数据源是通过DataContext来指定的
1. 最外层是Window是对象,Window的ItemsControl使用了ItemsTemplate,然后在ItemsTemplate中要绑定Language属性, 而整个Window的数据源是通 ...
- 01.WPF中制作无边框窗体
[引用:]http://blog.csdn.net/johnsuna/article/details/1893319 众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormB ...
- WPF中制作无边框窗体
原文:WPF中制作无边框窗体 众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormBorderStyle属性设置为None来完成.如果要制作成异形窗体,则需要使用图片或者使用G ...
- 整理:WPF中Binding的几种写法
原文:整理:WPF中Binding的几种写法 目的:整理WPF中Bind的写法 <!--绑定到DataContext--> <Button Content="{Bindin ...
随机推荐
- Linux服务管理之DHCP
1.DHCP服务简介 DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作, 主要有两个用途:给内部网络或网络 ...
- 安装docker后,导致qemu的桥接网络出现问题
按照Qemu-4.1 桥接网络设置中介绍的方法建立起桥接网络后,可以实现虚拟机和host的相互ping,但是在虚拟机里去ping其他跟host处于同一个网段的ip地址时却失败了,然后ifconfig后 ...
- JAVA类与类之间的关系及代码示例
参考链接:https://blog.csdn.net/wq6ylg08/article/details/81092056
- JSOI 2010 连通数
洛谷 P4306 [JSOI2010]连通数 洛谷传送门 题目描述 度量一个有向图联通情况的一个指标是连通数,指图中可达顶点对个的个数. 如图 顶点 11 可达 1,~2,~3,~4,~51, 2, ...
- c# 关于反射
反射的用途大体总结:1.使用Assembly定义和加载程序集,加载在程序集清单中列出模块,以及从程序集中查找类型并创建该类型的实例.CreateInstance2.使用Module了解包含模块的程序集 ...
- plv8 rpm包创建
以下是从一个三方rpm构建,获取到的rpm 包制作spec,主要是学习下pg 扩展rpm 包的打包 rpm src 包 下载地址 https://fedora.pkgs.org/29/fedora-x ...
- three.js 添加三维坐标系
//显示三维坐标系 ); scene.add(axis);
- 2-OpenResty 安装使用(Windows)
下载 OpenResty 1. https://gitee.com/yang456/LearnOpenResty.git 2. http://openresty.org/cn/download.h ...
- ESA2GJK1DH1K基础篇: 测试APP使用SmartConfig绑定Wi-Fi 设备并控制设备
前言 实现功能概要 STM32控制WI-Fi模块以AT指令TCP透传方式连接MQTT服务器, 实现MQTT通信控制. 测试准备工作(详细下载步骤请参考 硬件使用说明 ) 一,下载单片机程序 二,安装A ...
- 安装Visual Studio IntelliCode提供代码智能提示AI
The Visual Studio IntelliCode extension provides AI-assisted development features for Python, TypeSc ...