如何将视图层中的2个事件绑定到命令,以便为WPF中的按钮处理视图模型层中的2个事件? [英] How to binding 2 event in view layer to command so that handle 2 event in viewmodel layer for a button in WPF?

查看:65
本文介绍了如何将视图层中的2个事件绑定到命令,以便为WPF中的按钮处理视图模型层中的2个事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
在MVVM模型中使用命令时遇到问题.
在我的应用中,我有一个按钮,并且为此按钮引发了2个事件PreviewMouseLeftButtonDown和PreviewMouseLeftButtonUp.
我想将上述2个事件挂接到viewmodel类中的2个命令MouseLeftDownCommand和MouseLeftUpCommand中进行处理.

所以问题是:如何将2个命令挂接到相同控件的2个事件上?

请帮助我解决此问题.
谢谢和问候,

Hi all,
I''m meeting a problem when using command in MVVM model.
In my App, I have a button and I mst raise 2 event PreviewMouseLeftButtonDown and PreviewMouseLeftButtonUp for this button.
And I want to hook 2 above event into 2 command MouseLeftDownCommand and MouseLeftUpCommand in viewmodel class to handle.

So the question is: How to hook 2 command to 2 event of the same control ?

Please help me to resolve this problem.
Thanks and regards,

推荐答案



在您的viewmodel中,您将有两个命令:

Hi,

Within you viewmodel you''ll have two commands:

public ICommand MouseLeftDownCommand {get;set;}
public ICommand MouseLeftUpCommand {get;set;}



显然,您需要实例化它们并将它们连接到某些委托命令

然后,在您的视图(XAML页面)中,您可以执行以下操作:



Obviously, you''ll need to instantiate them and hook them up to some delegate command

Then, within you view (XAML page) you can do something like this:

<button>
  <button.inputbindings>
    <mousebinding gesture="LeftDownClick" command="{Binding MouseLeftDownCommand }" />
    <mousebinding gesture="LeftUpClick" command="{Binding MouseLeftUpCommand }" />
</button.inputbindings>
</button>



亲切的问候,



Kind regards,


这篇关于如何将视图层中的2个事件绑定到命令,以便为WPF中的按钮处理视图模型层中的2个事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆