整理: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 ...
随机推荐
- mysql 多条数据中,分组获取值最大的数据记录
摘要: 多条纪录中,几个字段相同,但是其中一个或者多个字段不同,则去该字段最大(这里只有一个不同) 源数据: 目的是移除:在同一天中只能存在一天数据,则取审核日期最大,数据库脚本如下: SELECT ...
- mysql 常用 sql 语句 - 快速查询
Mysql 常用 sql 语句 - 快速查询 1.mysql 基础 1.1 mysql 交互 1.1.1 mysql 连接 mysql.exe -hPup ...
- springBoot配置druid监控报错Failed to bind properties under 'spring.datasource.druid' to javax.sql.DataSource
报错信息: Description: Failed to bind properties under 'spring.datasource.druid' to javax.sql.DataSource ...
- A站(ACFun)爬虫爬取并播放、下载视频(JAVA)
本文使用的工具有vlc+ffmpeg+phantomjs 一般视频网站的视频地址暴露的方式有两种: 1.html直接暴露 2.通过ajax异步返回. A站使用的方式是第二种.针对第一种方式,直接使用j ...
- 目标检测论文解读3——Fast R-CNN
背景 deep ConvNet兴起,VGG16应用在图像分类任务上表现良好,本文用VGG16来解决检测任务.SPP NET存在CNN层不能fine tuning的缺点,且之前的方法训练都是分为多个阶段 ...
- Mac下搭建Appnium+Python+Ios模拟器环境
转载:https://www.jianshu.com/p/f7cf077d9444 https://blog.csdn.net/a158123/article/details/79684499 htt ...
- 1. vue 的安装
兼容性 Vue 不支持 IE8 及以下版本,因为 Vue 使用了 IE8 无法模拟的 ECMAScript 5 特性.但它支持所有兼容 ECMAScript 5 的浏览器. 安装: 1.直接用 < ...
- LNMP+memcached
部署LNMP+memcached网站平台,通过PHP页面实现对memcached服务器的数据操作,实现以下目标:部署LNMP实现PHP动态网站架构为PHP安装memcache扩展创建PHP页面,并编写 ...
- Hyperparameters
参数是机器学习算法的关键.它们通常由过去的训练数据中总结得出.在经典的机器学习文献中,我们可以将模型看作假设,将参数视为对特定数据集的量身打造的假设. 模型是否具有固定或可变数量的参数决定了它是否可以 ...
- ESA2GJK1DH1K基础篇: Android连接MQTT简单的Demo
题外话 我老爸也问我物联网发展的趋势是什么!!!!!! 我自己感觉的:(正在朝着 "我,机器人" 这部电影的服务器方向发展) 以后的设备都会和服务器交互,就是说本地不再做处理,全部 ...