使用MVVM模式处理控件上的鼠标事件 - 最佳做法 - [英] Handling Mouse events on controls with MVVM pattern - best practice -

查看:239
本文介绍了使用MVVM模式处理控件上的鼠标事件 - 最佳做法 - 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现实际上有两种使用mvvm模式处理鼠标事件的方法。

I found actually 2 ways to handle mouse events on controls with the mvvm pattern.

两种方式实际上是一种方式:

Both ways are actually 1 way:

http://mvvmlight.codeplex.com/ 的MVVM Light Toolkit

MVVM Light Toolkit by http://mvvmlight.codeplex.com/

<i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
        <cmd:EventToCommand
            Command="{Binding SelectionChangedCommand}"
            CommandParameter="{Binding SelectedItems,
                ElementName=MyDataGrid}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

和Blend interactiveivity.dll与行为

and the Blend interactivity.dll with Behaviours

<i:Interaction.Triggers>
  <i:EventTrigger EventName="MouseLeftButtonDown">
    <Behaviours:ExecuteCommandAction Command="{Binding MyCommand}" CommandParameter="{Binding MyCommandParameter}"/>
  </i:EventTrigger>
</i:Interaction.Triggers>

你知道更好的方法吗?

主持人:为什么我的最后6个xaml行的代码根本不可见?
它们被IE和Iron浏览器吞下。
您可以报告管理员来修复该代码脚本吗?它不常工作。证明: http://img251.imageshack.us/img251/5236/errorxt.png

Moderator: Why the heck are my last 6 xaml lines of code not visible at all? They are swallowed by IE and Iron browser. Would you please report the admin to fix that code script? its not working at all very often. prove: http://img251.imageshack.us/img251/5236/errorxt.png


推荐答案

如果您需要处理MouseDown,那么这两种方法都是很好的方式任意的地方。

Those are both good ways to do it if you need to handle MouseDown in arbitrary places.

然而,这些情况通常是很少的。通常有一个更简单的方法:

However these situations generally are few and far between. Usually there is a simpler way:


  • 你确定你的对象不是真的只是不像按钮的按钮吗?如果是这样,请将它们变成Button对象并对其进行模板,以满足您的需要。

  • 您确定您的对象只是列表中对象的选择区域吗?如果是这样,将容器从ItemsControl更改为ListBox,并将Restyle ListBoxItem更改为使用选择区域。

  • 您的对象是否正在选择图形路径?使用其内容是路径本身的ToggleButton。

还有很多其他例子。事实上,找到一个MouseDown映射到Command的情况并不是一个更干净的方法来做同样的事情是很常见的。

There are many other examples of this. In fact, it is uncommon to find a situation in which a MouseDown maps to a Command and there isn't a cleaner way to do the same thing.

这篇关于使用MVVM模式处理控件上的鼠标事件 - 最佳做法 - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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