使用 MVVM 模式处理控件上的鼠标事件 - 最佳实践 -
我实际上发现了两种使用 mvvm 模式处理控件上鼠标事件的方法。
两种方式实际上都是一种方式:
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 interactivity.dll 与 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要在任意位置处理 MouseDown,这些都是很好的方法。
然而,这些情况通常很少见。通常有一个更简单的方法:
这方面还有很多其他例子。事实上,将 MouseDown 映射到 Command 的情况并不常见,并且没有更简洁的方法来完成相同的操作。
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:
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.
总有另一种选择。您可以在 View 的代码隐藏中处理 WPF 事件,并在 ViewModel 上调用适当的方法。 MVVM模式并不禁止在View的代码隐藏文件中写入任何代码。
WPF 应用程序框架 (WAF) 的 ViewModel 示例应用程序> 展示了这是如何工作的。
There is always another option. You can handle WPF events in the code-behind of the View and call the appropriate method on the ViewModel. The MVVM pattern doesn't forbid to write any code in the code-behind file of the View.
The ViewModel sample application of the WPF Application Framework (WAF) shows how this can work.
XCommand 开源 codeplex 项目有更好的方法来处理基于 Command/CommandParameter 绑定的事件。在这里找到,xcommand.codeplex.com
下面是示例代码:
希望这会有所帮助。
XCommand Open source codeplex project has better way to deal with this event based Command/CommandParameter binding. Find here, xcommand.codeplex.com
Here is the sample code below:
Hope this will be helpful.