WPF中MVVM模式下控件自有的事件绑定
1、原因
在WPF中单纯的命令绑定往往不能满足覆盖所有的事件,例如ComboBox的SelectionChanged事件,DataGrid的SelectionChanged事件等等,这时就可以用事件绑定来完成。
2、示例
事件绑定需要用到:System.Windows. interactivity.dll 库,如果安装了Blend for visual studio,里面就包含了这个dll,在Interaction.Triggers里面添加一个或多个EventTrigger并指定关注的的事件名称,在EventTrigger中通过InvokeCommandAction来绑定事件对应的命令。
代码如下:
<ComboBox Grid.Column="" Width=""
SelectedItem="{Binding SelectConfirmStatus, Mode=TwoWay}" ItemsSource="{Binding ConfirmStatus, Mode=OneWay}" SelectedValuePath="ID"
DisplayMemberPath="StatusName" Height="" Foreground="Black"
HorizontalContentAlignment="Left" VerticalContentAlignment="Center"
HorizontalAlignment="Right" VerticalAlignment="Center" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding ConfirmStatusCommand}">
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
同时需要引入命名空间,如下:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
然后再ViewModel中实现该命令即可,如:
public RelayCommand<object> ConfirmStatusCommand { private set; get; }
在类的构造函数中实现该命令属性:
ConfirmStatusCommand = new RelayCommand<object>(args =>
{
CheckAllotOrders();//此为命令执行的方法
});
整体就这样,供参考,但此方法也有局限,就是不能把动态的命令参数(如键盘按键事件,获取按的键值参数)传入ViewModel,如需传参数,还得实现一个参数类。
WPF中MVVM模式下控件自有的事件绑定的更多相关文章
- WindowsXamlHost:在 WPF 中使用 UWP 的控件(Windows Community Toolkit)
Windows Community Toolkit 再次更新到 5.0.以前可以在 WPF 中使用有限的 UWP 控件,而现在有了 WindowsXamlHost,则可以使用更多 UWP 原生控件了. ...
- 在WPF中添加Windows Form控件(包括 ocx控件)
首先,需要向项目中的reference添加两个dll,一个是.NET库中的System.Windows.Forms,另外一个是WindowsFormsIntegration,它的位置一般是在C:\ ...
- 当前页面刷新和动态添加控件的jquery事件绑定on
当前页面刷新(console): location.reload() 给动态添加的控件添加js事件(委托): <ul> <li>菜单一</li> <li> ...
- 笔记03 wpf 在MVVM模式下怎样在Viewmodel里面获得view的控件对象
转自http://blog.csdn.net/qing2005/article/details/6601199http://blog.csdn.net/qing2005/article/detail ...
- WPF中MVVM模式的 Event 处理
WPF的有些UI元素有Command属性可以直接实现绑定,如Button 但是很多Event的触发如何绑定到ViewModel中的Command呢? 答案就是使用EventTrigger可以实现. 继 ...
- WPF 中如何使用第三方控件 ,可以使用WindowsFormsHost 类
允许在 WPF 页面上承载 Windows Forms控件的元素. 命名空间: System.Windows.Forms.Integration 程序集: WindowsFormsIntegr ...
- WPF中通过代码设置控件的坐标
用WPF做贪吃蛇小游戏时,发现了一个问题: 贪吃蛇的移动,我是通过不断刷新Rectangle来实现(贪吃蛇的身体由一组Rectangle组成),因此需要不断调整Rectangle的坐标,但是WPF中没 ...
- WPF中 MVVM模式的Slider Binding.
对于Button的Command的绑定可以通过实现ICommand接口来进行,但是Slider并没有Command属性. 另外如果要实现MVVM模式的话,需要将一些Method和Slider的Even ...
- 在WPF中引用WinForm的控件
以ArcEngine为例: mapControl = new AxMapControl(); MapHost.Child = mapControl; //MapHost为WindowsFormHos ...
随机推荐
- django -- url 的 name 属性
在html的form中使用给url定义的name值,可以在修改url时不用在修改form的src. urls.py from django.conf.urls import url from myte ...
- Betsy's Tour 漫游小镇(dfs)
Description 一个正方形的镇区分为 N2 个小方块(1 <= N <= 7).农场位于方格的左上角,集市位于左下角.贝茜穿过小镇,从左上角走到左下角,刚好经过每个方格一次.当 N ...
- Hive split 分割函数
hive字符串分割函数 split(str, regex) - Splits str around occurances that match regexTime taken: 0.769 secon ...
- Spring MVC的配置
一.添加依赖 <dependency> <groupId>org.springframework</groupId> <artifactId>sprin ...
- Redis搭建(二):主从复制
一.引言 Redis有三种集群模式: 第一个就是主从模式 第二种“哨兵”模式,在Redis 2.6版本开始提供,2.8版本稳定 第三种是Cluster集群模式,在Redis 3.x以后的版本才增加进来 ...
- ora-28547:连接服务器失败,可能是 Oracle Net 管理失败
检查如下: 监听程序的配置文件 发现多了 (PROGRAM = extproc) 去掉后如下: # listener.ora Network Configuration \dbhome_1\netwo ...
- python子进程模块subprocess详解与应用实例 之三
二.应用实例解析 2.1 subprocess模块的使用 1. subprocess.call >>> subprocess.call(["ls", " ...
- 【bzoj1602】[Usaco2008 Oct]牧场行走
1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1793 Solved: 935[Submit][St ...
- 张超超OC基础回顾04_实例变量修饰(@public),点语法,self关键字,多态,继承
零.实例变量修饰符 /* @public 就是实例变量修饰符 @public >可以在其它类中访问被public修饰的成员变量 >也可以在本类中访问被public修饰的成员变量 >可 ...
- 2、python的print函数格式化输出
1.格式化输出浮点数(float) pi = 3.141592653 print('%10.3f' % pi) #字段宽10,精度3 ,结果 print('%010.3f' % pi) #用0填充 ...