整理: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 ...
随机推荐
- 集成学习 - Bagging
认识 Bagging 的全称为 (BootStrap Aggregation), 嗯, 咋翻译比较直观一点呢, 就有放回抽样 模型训练? 算了, 就这样吧, 它的Paper是这样的: Algorith ...
- ansible自动化运维04
ansible playbook ansible-playbook命令格式: ansible-playbook [option] filename(剧本名字) ansible-playbook 命 ...
- MMORPG服务器场景中的aoi算法思考
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/lwtbn1/article/details/37961695 最近在做一个移动平台上的MMORPG项 ...
- 网络编程 TCP协议:三次握手,四次回收,反馈机制 socket套接字通信 粘包问题与解决方法
TCP协议:传输协议,基于端口工作 三次握手,四次挥手 TCP协议建立双向通道. 三次握手, 建连接: 1:客户端向服务端发送建立连接的请求 2:服务端返回收到请求的信息给客户端,并且发送往客户端建立 ...
- [KCOJ3393]上马
题目描述 Description Chicken在IEC(International Equestrianism Competition(国际马术表演赛))惨跪,没有成功的上到马,他深刻的记得他的选手 ...
- redis主从+redis的哨兵模式
三台机器分布 192.168.189.129 // master的角色 192.168.189.130 // slave1的角色 192.168.189.131 // salve2的角色 ...
- Python面向对象 | 类方法 classmethod
类方法:必须通过类的调用,而且此方法的意义:就是对类里面的变量或者方法进行修改添加. 例一个商店,店庆全场八折,代码怎么写呢? class Goods: __discount = 0.8 # 折扣 ...
- Pandas | GroupBy 分组
任何分组(groupby)操作都涉及原始对象的以下操作之一: 分割对象 应用一个函数 结合的结果 在许多情况下,我们将数据分成多个集合,并在每个子集上应用一些函数.在应用函数中,可以执行以下操作: 聚 ...
- 将HashMap转换为List
背景 SpringBoot中,使用@RquestBody注解 hashMap 接收多个参数的json字符串数据,包括一个数组和一个int值.数组中为一个个的对象组成. 问题 使用 map.ge ...
- pychram-redis破解
1. Preferences -> Plugins-> 选择右下角Browse repositories 2. 搜索Iedis 3. 找到Iedis插件目录:C:\Users\用户名\.P ...