整理: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 ...
随机推荐
- Oracle PLSQL游标、游标变量的使用
参考文章:https://www.cnblogs.com/huyong/archive/2011/05/04/2036377.html 在 PL/SQL 程序中,对于处理多行记录的事务经常使用游标来实 ...
- HDU 2612 Find a way 题解
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Centos6.5 下安装 tmux(免编译)
环境:Centos6.5是新安装的,没有安装过其它的软件包.思路是安装epel的源后再安装tmux. yum install epel-release # 安装Linux的 epel 的yum源 yu ...
- java.sql.SQLException: Access denied for user 'xxx'@'localhost' (using password: YES)
java.sql.SQLException: Access denied for user 'xxx'@'localhost' (using password: YES) at com.mysql.c ...
- nginx 跨域请求访问
1.nginx跨域请求访问 location ~ .*\.(htm|html)$ { add_header Access-Control-Allow-Origin(请求域名) *(所有域名) http ...
- HTML元素脱离文档流的三种方法
一.什么是文档流? 将窗体自上而下分成一行一行,并在每行中按从左至右依次排放元素,称为文档流,也称为普通流. 这个应该不难理解,HTML中全部元素都是盒模型,盒模型占用一定的空间,依次排放在HTML中 ...
- #51nod上topcoder练习记
好久没刷51nod了,又听说topcoder有很多好题.那么就来51nod上刷吧.(那个客户端搞得有点烦(看不懂)) [1366 贫富差距] 当图不连通的时候,答案为无穷大. 当图连通时,两个点之间的 ...
- ansible-playbook实例
准备前提 配置ansible主机详情:https://www.cnblogs.com/security-guard/ nginx的安装 编写nginx的自动部署文件nginx.yml hos ...
- Pandas | 01 数据结构
Pandas的三种数据结构: 系列(Series) 数据帧(DataFrame) 面板(Panel) 这些数据结构,构建在Numpy数组之上,这意味着它们很快 维数和描述 考虑这些数据结构的最好方法是 ...
- Rpm Creating Subpackages
转自:http://ftp.rpm.org/max-rpm/s1-rpm-subpack-spec-file-changes.html Spec File Changes For Subpackage ...