事件绑定意义

一般事件的处理程序都放在界面后台,通过事件绑定可以把事件处理程序放在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. 迷宫问题(DFS)

    声明:图片及内容基于https://www.bilibili.com/video/BV1oE41177wk?t=3245 问题及分析 8*8的迷宫,最外周是墙,0表示可以走,1表示不可以走 设置迷宫 ...

  2. 【odoo14】第八章、服务侧开发-进阶

    本章代码位于作为GITHUB库 https://github.com/PacktPublishing/Odoo-14-Development-Cookbook-Fourth-Edition 在第五章( ...

  3. BZOJ_2115 [Wc2011] Xor 【图上线性基】

    一.题目 [Wc2011] Xor 二.分析 比较有意思的一题,这里也学到一个结论:$1$到$N$的任意一条路径异或和,可以是一个任意一条$1$到$N$的异或和与图中一些环的异或和组合得到.因为一个数 ...

  4. javascript中的Strict模式

    目录 简介 使用Strict mode strict mode的新特性 强制抛出异常 简化变量的使用 简化arguments 让javascript变得更加安全 保留关键字和function的位置 总 ...

  5. 微信小程序Animation动画的使用

    目录 1,前言 2,属性 3,使用 1,前言 和css3动画不同,小程序中动画是主要是通过js控制的,简单来说就是创建一个动画实例animation.调用实例的方法来定义动画效果.最后通过动画实例的e ...

  6. Hadoop企业开发场景案例,虚拟机服务器调优

    Hadoop企业开发场景案例 1 案例需求 ​ (1)需求:从1G数据中,统计每个单词出现次数.服务器3台,每台配置4G内存,4核CPU,4线程. ​ (2)需求分析: ​ 1G/128m = 8个M ...

  7. 第30 章 : 理解 RuntimeClass 与使用多容器运行时

    理解 RuntimeClass 与使用多容器运行时 本文将主要分享以下三方面的内容: RuntimeClass 需求来源 RuntimeClass 功能介绍 多容器运行时示例 RuntimeClass ...

  8. 以聊天的形式解决traefik2.1.X的一个问题

    海口-老男人 17:24:48 大哥,这个是啥报错呀 海口-老男人 17:27:04 E0413 09:23:13.134144 1 reflector.go:153] pkg/mod/k8s.io/ ...

  9. 【C/C++】面向对象开发的优缺点

    原创文章,转发请注明出处. 面向对象开发的优缺点 面向对象开发 是相对于 面向过程开发 的一种改进思路. 由于流水线式的面相过程开发非常直接,高效.在面对一些简单项目时,只需要几百行,甚至是几十行代码 ...

  10. 黑马架构师v2.5.1 (codeUtil)使用注意事项

    资源 1.资料里的codeutil软件有问题,使用时部分类和接口文件名后有一串日期数字等.码云的没问题 2.生成代码后zookeeper的IP改为本机的