WPF中MVVM模式的 Event 处理

WPF的有些UI元素有Command属性可以直接实现绑定,如Button

但是很多Event的触发如何绑定到ViewModel中的Command呢?

答案就是使用EventTrigger可以实现。

继续上一篇对Slider的研究,在View中修改Interaction.

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

那么如果将EventName修改为Thumb.DragCompleted 后发现这个事件并不会被触发
原因是:Because the command is hooked up to the Slider, but the event is fired on the Thumb。

(参考:http://stackoverflow.com/questions/14331272/issue-with-thumb-dragstarted-event-with-mvvmlight)

参考上述链接中Tom Allen的方法后可以实现, 但是这个方法并没有很好的遵守MVVM模式。

于是接着研究,既然DragCompleted是挂在Thumb上面的,那么为何不直接和Thumb 绑定呢?

 

修改Slider的ControlTemplate, 在Track控件中的Thumb中绑定Event 成功!

 

    <UserControl.Resources><ControlTemplate x:Key="trackThumb" TargetType="{x:Type Slider}"><Border Background="{TemplateBinding Background}"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"><Grid><Track x:Name="PART_Track"><Track.Thumb><Thumb Width="10"><i:Interaction.Triggers><i:EventTrigger EventName="DragCompleted"><i:InvokeCommandAction Command="{Binding ValueChangedCommand}" /></i:EventTrigger></i:Interaction.Triggers></Thumb></Track.Thumb></Track></Grid></Border></ControlTemplate></UserControl.Resources>

 参考:

http://social.technet.microsoft.com/wiki/contents/articles/18199.event-handling-in-an-mvvm-wpf-application.aspx

http://www.codeproject.com/Articles/274982/Commands-in-MVVM#example9

转载于:https://www.cnblogs.com/DreamRecorder/p/9849986.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388135.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Eclipse 插件开发 向导

阅读目录 最近由于特殊需要&#xff0c;开始学习插件开发。   下面就直接弄一个简单的插件吧!   1 新建一个插件工程   2 创建自己的插件名字&#xff0c;这个名字最好特殊一点&#xff0c;一遍融合到eclipse的时候&#xff0c;不会发生冲突。   3 下一步&#xff0c;进…

线性回归 假设_线性回归的假设

线性回归 假设Linear Regression is the bicycle of regression models. It’s simple yet incredibly useful. It can be used in a variety of domains. It has a nice closed formed solution, which makes model training a super-fast non-iterative process.线性回归是回…

ES6模块与commonJS模块的差异

参考&#xff1a; 前端模块化 ES6 在语言标准的层面上&#xff0c;实现了模块功能&#xff0c;而且实现得相当简单&#xff0c;旨在成为浏览器和服务器通用的模块解决方案。 其模块功能主要由两个命令构成&#xff1a;export和import。export命令用于规定模块的对外接口&#x…

solo

solo - 必应词典 美[soʊloʊ]英[səʊləʊ]n.【乐】独奏(曲)&#xff1b;独唱(曲)&#xff1b;单人舞&#xff1b;单独表演adj.独唱[奏]的&#xff1b;单独的&#xff1b;单人的v.独奏&#xff1b;放单飞adv.独网络梭罗&#xff1b;独奏曲&#xff1b;索罗变形复数&#xff1…

Eclipse 简介和插件开发天气预报

Eclipse 简介和插件开发 Eclipse 是一个很让人着迷的开发环境&#xff0c;它提供的核心框架和可扩展的插件机制给广大的程序员提供了无限的想象和创造空间。目前网上流传相当丰富且全面的开发工具方面的插件&#xff0c;但是 Eclipse 已经超越了开发环境的概念&#xff0c;可以…

趣味数据故事_坏数据的好故事

趣味数据故事Meet Julia. She’s a data engineer. Julia is responsible for ensuring that your data warehouses and lakes don’t turn into data swamps, and that, generally speaking, your data pipelines are in good working order.中号 EETJulia。 她是一名数据工程…

Linux 4.1内核热补丁成功实践

最开始公司运维同学反馈&#xff0c;个别宿主机上存在进程CPU峰值使用率异常的现象。而数万台机器中只出现了几例&#xff0c;也就是说万分之几的概率。监控产生的些小误差&#xff0c;不会造成宕机等严重后果&#xff0c;很容易就此被忽略了。但我们考虑到这个异常转瞬即逝、并…

python分句_Python循环中的分句,继续和其他子句

python分句Python中的循环 (Loops in Python) for loop for循环 while loop while循环 Let’s learn how to use control statements like break, continue, and else clauses in the for loop and the while loop.让我们学习如何在for循环和while循环中使用诸如break &#xf…

eclipse plugin 菜单

简介&#xff1a; 菜单是各种软件及开发平台会提供的必备功能&#xff0c;Eclipse 也不例外&#xff0c;提供了丰富的菜单&#xff0c;包括主菜单&#xff08;Main Menu&#xff09;&#xff0c;视图 / 编辑器菜单&#xff08;ViewPart/Editor Menu&#xff09;和上下文菜单&am…

[翻译 EF Core in Action 2.0] 查询数据库

Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Core 书籍。原版地址. 是除了官方文档外另一个学习EF Core的不错途径, 书中由浅入深的讲解的EF Core的相关知识。因为没有中文版,所以本人对其进行翻译。 预计每两天…

hdu5692 Snacks dfs序+线段树

题目传送门 题目大意&#xff1a;给出一颗树&#xff0c;根节点是0&#xff0c;有两种操作&#xff0c;一是修改某个节点的value&#xff0c;二是查询&#xff0c;从根节点出发&#xff0c;经过 x 节点的路径的最大值。 思路&#xff1a;用树状数组写发现还是有些麻烦&#xff…

python数据建模数据集_Python中的数据集

python数据建模数据集There are useful Python packages that allow loading publicly available datasets with just a few lines of code. In this post, we will look at 5 packages that give instant access to a range of datasets. For each package, we will look at h…

打开editor的接口讨论

【打开editor的接口讨论】 先来看一下workbench吧&#xff0c;workbench从静态划分应该大致如下&#xff1a; 从结构图我们大致就可以猜测出来&#xff0c;workbench page作为一个IWorkbenchPart&#xff08;无论是eidtor part还是view part&#…

【三角函数】已知直角三角形的斜边长度和一个锐角角度,求另外两条直角边的长度...

如图,已知直角三角形ABC中,∠C90, ∠Aa ,ABc ,求直角边AC、BC的长度. ∵ ∠C90,∠Aa ,ABc ,Cos∠AAC/AB ,Sin∠ABC/AB ,∴ ACAB*Cos∠Ac*Cosa ,BCAB*Sin∠Ac*Sina . 复制代码

网络攻防技术实验五

2018-10-23 实验五 学 号201521450005 中国人民公安大学 Chinese people’ public security university 网络对抗技术 实验报告 实验五 综合渗透 学生姓名 陈军 年级 2015 区队 五 指导教师 高见 信息技术与网络安全学院 2018年10月23日 实验任务总纲 2018—2019 …

usgs地震记录如何下载_用大叶草绘制USGS地震数据

usgs地震记录如何下载One of the many services provided by the US Geological Survey (USGS) is the monitoring and tracking of seismological events worldwide. I recently stumbled upon their earthquake datasets provided at the website below.美国地质调查局(USGS)…

Springboot 项目中 xml文件读取yml 配置文件

2019独角兽企业重金招聘Python工程师标准>>> 在xml文件中读取yml文件即可&#xff0c;代码如下&#xff1a; 现在spring-boot提倡零配置&#xff0c;但是的如果要集成老的spring的项目&#xff0c;涉及到的bean的配置。 <bean id"yamlProperties" clas…

eclipse 插件打包发布

如果想把调试好的插件打包发布&#xff0c;并且在ECLIPSE中可以使用. 1.File-->Export 2.选择 PLug-in Development下 的 Deployable plug-ins and fragments 3.进入 Deployable plug-ins and fragments 页面 4.把底下的 Destubatuib 的选项中选择 Archive file 在这里添入要…

无法获取 vmci 驱动程序版本: 句柄无效

https://jingyan.baidu.com/article/a3a3f811ea5d2a8da2eb8aa1.html 将 vmci0.present "TURE" 改为 “FALSE”; 转载于:https://www.cnblogs.com/limanjihe/p/9868462.html

数据可视化 信息可视化_更好的数据可视化的8个技巧

数据可视化 信息可视化Ggplot is R’s premier data visualization package. Its popularity can likely be attributed to its ease of use — with just a few lines of code you are able to produce great visualizations. This is especially great for beginners who are…