事件绑定意义

一般事件的处理程序都放在界面后台,通过事件绑定可以把事件处理程序放在ViewModel中,实现界面和逻辑的解耦

要使用事件绑定需要借助System.Windows.interactivity(安装了Blend就有),如果电脑上找不到,可以通过NuGet安装System.Windows.Interactivity.WPF

需要引用以下命名空间:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
using System.Windows.Interactivity;

无参数的事件绑定

Interaction.Triggers里面添加一个或多个EventTrigger并指定关注的的事件名称,在EventTrigger中通过InvokeCommandAction来绑定事件对应的命令,在事件触发后会调用绑定的命令对象的Execute方法执行命令。

命令的实现参考WPF之自定义委托命令,事件触发后是否能够真正执行绑定的命令也受到绑定的命令的CanExecute方法的影响。事件绑定过程如下:

<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>

带EventArgs参数的事件绑定

InvokeCommandAction在未对CommandParameter绑定的情况下给Execute方法传递的参数为null,对CommandParameter绑定的情况下给Execute方法传递的参数为绑定值(不是EventArgs参数),Execute方法是由Invoke(object parameter)调用的

其实,TiggerBase调用InvokeCommandAction的Invoke(object parameter)方法时有传入EventArgs参数,但Invoke调用Execute方法时一直使用的是CommandParameter参数。有一说一,这个程序逻辑有点反人类,这也是网上为什么有这么多重新实现InvokeCommandAction资料的原因。

如果需要从事件的EventArgs中获取数据,正常来说派生InvokeCommandAction然后“重写”Invoke方法即可。但是,InvokeCommandAction是密封类,我们只能参照源码重新实现一个EventCommandAction类,代码如下:

using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity; namespace WpfApp
{
public class EventCommandAction : TriggerAction<DependencyObject>
{
/// <summary>
/// 事件要绑定的命令
/// </summary>
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
} // Using a DependencyProperty as the backing store for MsgName. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(EventCommandAction), new PropertyMetadata(null)); /// <summary>
/// 绑定命令的参数,保持为空就是事件的参数
/// </summary>
public object CommandParameter
{
get { return (object)GetValue(CommandParateterProperty); }
set { SetValue(CommandParateterProperty, value); }
} // Using a DependencyProperty as the backing store for CommandParateter. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandParateterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(EventCommandAction), new PropertyMetadata(null)); //执行事件
protected override void Invoke(object parameter)
{
if (CommandParameter != null)
parameter = CommandParameter;
var cmd = Command;
if (cmd != null&&cmd.CanExecute(parameter))
cmd.Execute(parameter);
}
}
}

使用事件绑定

创建一个MainViewModel,这里要使用之前的数据绑定基类BindableBase(参考WPF之数据绑定基类),代码如下:

class MainViewModel:BindableBase
{
public bool CanExecute { get; set; } private string tipText;
public string TipText
{
get { return tipText; }
set { SetProperty(ref tipText, value); }
} public DelegateCommand LoadedCommand { get; } public DelegateCommand<MouseEventArgs> MouseMoveCommand { get; } public MainViewModel()
{
LoadedCommand = new DelegateCommand(() => { MessageBox.Show("程序加载成功");}); MouseMoveCommand = new DelegateCommand<MouseEventArgs>(e =>
{
TipText = "鼠标当前位置:" + e.GetPosition(e.Device.Target).ToString();
},
e =>CanExecute);
}
}

界面的XAML代码如下:

<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadedCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseMove">
<local:EventCommandAction Command="{Binding MouseMoveCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<StackPanel>
<CheckBox Content="命令开关" IsChecked="{Binding CanExecute}"/>
<Label Content="{Binding TipText}" Margin="5"/>
</StackPanel>

在后台代码中添加DataContext:

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
}

运行程序,效果如下:



扩展:基于InvokeCommandAction源码的实现(推荐)

可以在InvokeCommandAction源码基础改动一下Invoke方法,实现我们的需求,改动如下:

protected override void Invoke(object parameter)
{
if (base.AssociatedObject != null)
{
if (CommandParameter != null)
parameter = CommandParameter;
ICommand command = ResolveCommand();
if (command != null && command.CanExecute(parameter))
{
command.Execute(parameter);
}
}
}

重写的RewriteInvokeCommandAction类完全可以替代上面的EventCommandAction,完整版代码如下:

public sealed class RewriteInvokeCommandAction : TriggerAction<DependencyObject>
{
private string commandName; public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(RewriteInvokeCommandAction), null); public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(RewriteInvokeCommandAction), null); public string CommandName
{
get
{
ReadPreamble();
return commandName;
}
set
{
if (CommandName != value)
{
WritePreamble();
commandName = value;
WritePostscript();
}
}
} public ICommand Command
{
get
{
return (ICommand)GetValue(CommandProperty);
}
set
{
SetValue(CommandProperty, value);
}
} public object CommandParameter
{
get
{
return GetValue(CommandParameterProperty);
}
set
{
SetValue(CommandParameterProperty, value);
}
} protected override void Invoke(object parameter)
{
if (base.AssociatedObject != null)
{
if (CommandParameter != null)
parameter = CommandParameter;
ICommand command = ResolveCommand();
if (command != null && command.CanExecute(parameter))
{
command.Execute(parameter);
}
}
} private ICommand ResolveCommand()
{
ICommand result = null;
if (Command != null)
{
result = Command;
}
else if (base.AssociatedObject != null)
{
Type type = base.AssociatedObject.GetType();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
PropertyInfo[] array = properties;
foreach (PropertyInfo propertyInfo in array)
{
if (typeof(ICommand).IsAssignableFrom(propertyInfo.PropertyType) && string.Equals(propertyInfo.Name, CommandName, StringComparison.Ordinal))
{
result = (ICommand)propertyInfo.GetValue(base.AssociatedObject, null);
}
}
}
return result;
}
}

参考资料

MVVM设计模式和在WPF中的实现(四)事件绑定

EventTrigger原理浅谈

WPF:MVVM:命令与CallMethodAction?

WPF之事件绑定命令的更多相关文章

  1. WPF中使用System.Windows.Interactivity实现事件绑定的替代方法

    一.问题描述 对于 Button 等控件,在 MVVM 中我们能通过 Command 绑定解决 Click 事件.具体如下所示: <Button Margin="10" He ...

  2. WPF 在事件中绑定命令(不可以在模版中绑定命令)

    其实这也不属于MVVMLight系列中的东东了,没兴趣的朋友可以跳过这篇文章,本文主要介绍如何在WPF中实现将命令绑定到事件中. 上一篇中我们介绍了MVVMLight中的命令的用法,那么仅仅知道命令是 ...

  3. WPF 在事件中绑定命令

    导航:MVVMLight系列文章目录:<关于 MVVMLight 设计模式系列> 其实这也不属于MVVMLight系列中的东东了,没兴趣的朋友可以跳过这篇文章,本文主要介绍如何在WPF中实 ...

  4. 整理:WPF中应用附加事件制作可以绑定命令的其他事件

    原文:整理:WPF中应用附加事件制作可以绑定命令的其他事件 目的:应用附加事件的方式定义可以绑定的事件,如MouseLeftButton.MouseDouble等等 一.定义属于Control的附加事 ...

  5. 整理:WPF用于绑定命令和触发路由事件的自定义控件写法

    原文:整理:WPF用于绑定命令和触发路由事件的自定义控件写法 目的:自定义一个控件,当点击按钮是触发到ViewModel(业务逻辑部分)和Xaml路由事件(页面逻辑部分) 自定义控件增加IComman ...

  6. MVVM设计模式和WPF中的实现(四)事件绑定

    MVVM设计模式和在WPF中的实现(四) 事件绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  7. WPF Event 在 Command 中的应用初级篇,支持所有Event 展示松耦合设计的全部代码 - 解决TextBoxBase.TextChanged或者TextBox.TextChanged等类似事件绑定问题。

    做过WPF开发的人,都知道做MVVM架构,最麻烦的是Event的绑定,因为Event是不能被绑定的,同时现有的条件下,命令是无法替代Event.而在开发过程中无法避免Event事件,这样MVVM的架构 ...

  8. WPF采用MVVM模式(绑定:纯前台、命令:触发器绑定命令)

    MVVM绑定 view-viewModel-model,模型介绍省略,就是创建类,添加字段封装属性.注:控件的绑定只能绑定到属性上,不能绑定到字段上: 接下来就是代码 (view): <Wind ...

  9. MVVM设计模式和在WPF中的实现(四) 事件绑定

    系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中的实现(三)命令绑定 MVVM模式解析和在WPF中的 ...

随机推荐

  1. Java 获取小程序openid(基于SpringBoot)

    Java 获取小程序openid(基于SpringBoot) 官方文档 wx.login 1.引入Request封装依赖 <!--Request依赖--> <dependency&g ...

  2. 【python+selenium的web自动化】- 控制浏览器的常用操作

    如果想从头学起selenium,可以去看看这个系列的文章哦! https://www.cnblogs.com/miki-peng/category/1942527.html 前言 ​ 本文主要介绍se ...

  3. Go语言学习笔记——Go语言的指针

    Go具有指针.指针保存了变量的内存地址. 类型*T是指向类型T的值得指针.其零值是nil var p *int &符号会生成一个指向其作用对象的指针 i:=42 P=&i *符号表示指 ...

  4. Codeforces 1015E1 Stars Drawing (Easy Edition)

    题面: 传送门 题目描述: 要求用十字星星来画题目给出的"星"图.如果不能用十字星星来画"星"图,输出-1:如果能,则输出要在图的哪个位置画相应大小的十字星图. ...

  5. vue全家桶和react全家桶

    vue全家桶:vue  +  vuex (状态管理)  + vue-router (路由) + vue-resource +axios +elementui react全家桶 : react + re ...

  6. 让你的浏览器变成Siri一样的语音助手

    最近业余时间浏览技术文章的时候,看到了一篇关于语音朗读的文章:Use JavaScript to Make Your Browser Speak(用Javascript让你的浏览器说话),文章中提到可 ...

  7. LayUi表单模块无法正常显示

    问题: 当我们再使用LayUI的Form表单模块时,我们会把自己需要的表单赋值到我们的页面中,但是会出现无法正常显示的问题,如下: 出现原因: LayUI官方文档也明确表示:"当你使用表单时 ...

  8. x86汇编 条件跳转

    条件跳转表 汇编语言-条件跳转指令 直接转移指令 指令格式 机器码 测试标志 条件说明 符号  JO       OPR 70  OF=1  结果有溢出    JNO      OPR 71  OF= ...

  9. 5、Spring教程之依赖注入

    概念 依赖注入(Dependency Injection,DI). 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 . 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装 ...

  10. RabbitMQ 入门 (Go) - 2. 发布和接收消息

    本文我将使用 Go 语言在 RabbitMQ 上发布和接收消息. Go 的标准库本身并没有 RabbitMQ 的原生绑定,但是有一个第三方库确能够支持 RabbitMQ,它的源码在 https://g ...