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

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

问题描述



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



MVVM Light Toolkit由 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 interactivity.dll

 < i:Interaction.Triggers> 
< i:EventTrigger EventName =MouseLeftButtonDown>
< Behaviors:ExecuteCommandAction Command ={Binding MyCommand}CommandParameter ={Binding MyCommandParameter}/>
< / i:EventTrigger>
< / i:Interaction.Triggers>你知道更好的方法吗?

blockquote>

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



解决方案

如果你需要处理MouseDown在任意地方。



但是这些情况通常是很少和很远的。通常有一个更简单的方法:




  • 你确定你的对象不是真的按钮,看起来不像按钮?

  • 您确定您的对象只是列表中对象的选择区域吗?如果是,将容器从ItemsControl更改为ListBox,然后重新设置ListBoxItem以使用选择区域。

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



还有很多其他的例子。事实上,很少发现一个情况,一个MouseDown映射到一个命令,没有一个更清洁的方式做同样的事情。


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

Both ways are actually 1 way:

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>

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>

Do you know of any better method?

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

解决方案

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:

  • Are you sure your objects aren't really buttons that just don't look like buttons? If so, make them real Button objects and template them to look the way you want.
  • Are you sure your objects are just selection areas for objects in a list? If so, change the container from ItemsControl to ListBox and restyle ListBoxItem to use the selection areas.
  • Are your objects graphical paths that are being selected? Use a ToggleButton whose content is the path itself.

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天全站免登陆