如何向控件的事件添加新命令 [英] How Can I Add a New Command to a Control's Event

查看:36
本文介绍了如何向控件的事件添加新命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您看这个问题.

我一直在按照 Josh Smith 的 MVVM 模式示例构建应用程序 (VB/WPF):点击查看

I have been building an application (VB/WPF) following Josh Smith's example of an MVVM pattern: Click here to view

我的视图具有控件,可根据事件触发命令,例如按钮单击,并且这些控件绑定到 ViewModel 中的属性.

My Views have Controls which trigger a Command upon an event, such as a button click and these are bound to properties in the ViewModel.

我的 ViewModel 公开 ICommand 类型的属性,这些属性调用适当的方法.

My ViewModel exposes properties of type ICommand which invoke the appropriate methods.

到目前为止效果很好!我现在想使用 Thumb Control 来允许用户在 Canvas 中移动对象.我不相信 Thumb 具有开箱即用"的命令功能.移动 Thumb 时会引发DragDelta"事件.所以我的问题如下:

This has worked well, until now! I now want to use a Thumb Control to allow a user to move an object within a Canvas. I don't believe that the Thumb has a Command capability 'out of the box'. A Thumb raises a 'DragDelta' event when being moved. So my question is as follows:

我如何扩展控件的功能以在事件发生时发出命令?

How do I extend a Control's capability to issue a Command upon an event please?

我想发出一个新的命令,比如说onDragDelta",这样我就可以将它绑定到我的 ViewModel 中的一个属性,就像我的按钮一样.

I wish to issue a new Command, let's say 'onDragDelta' so that I can bind it to a property in my ViewModel just like my Buttons.

非常感谢

推荐答案

您想要的是基于 FrameworkElement 的 RoutedEvent 在您的 ViewModel 中触发 ICommand.不幸的是,WPF 不支持这种开箱即用".一些可能性:

What you want is to fire an ICommand in your ViewModel based on a FrameworkElement's RoutedEvent. Unfortunately, WPF doesn't support this "out-of-box". Some possibilities:

1) 使用 Expression Blend 3,可以使用 Microsoft.Expression.Interactivity.dll.有关如何执行此操作的示例,请参阅 WPF:混合 3 交互/行为.

1) With Expression Blend 3 there is Microsoft.Expression.Interactivity.dll, which allows this. See WPF : Blend 3 Interactions / Behaviours for an example on how to do this.

2) 您的代码隐藏中可以有一个事件处理程序,它直接调用适当的 ViewModel 命令.例如:

2) You could have an event handler in your code-behind, which directly calls the appropriate ViewModel command. For example:

private void FrameworkElement_DragDelta(object sender, EventArgs e)
{
    this.MyViewModel.OnDragDelta(sender, e);
}

3) 作为 Cinch 框架的一部分,有一个 附加命令行为.这样,您就不必更改您的代码隐藏.例如:

3) As part of the Cinch framework, there is an attached command behaviour. That way, you do not have to change your code-behind. For example:

Cinch:SingleEventCommand.RoutedEventName="DragDelta"
Cinch:SingleEventCommand.TheCommandToRun="{Binding Path=DragDeltaCommand}"

请注意,这会调用 ICommand 并将对象(实际上是 SCommandArgs)作为命令参数传递.要在命令处理程序中获取实际路由事件参数,请执行以下操作:

Note that this calls an ICommand and passes an object (actually an SCommandArgs) as command parameter. To get the actual routed event arguments in your command handler, do the following:

var sargs = args as SCommandArgs;
if (sargs == null)
{
    return;
}

var routedEventArgs = sargs.EventArgs as RoutedEventArgs; // Or whatever arguments you actually expect
if (routedEventArgs == null)
{
    return;
}

// Do something here with the event arguments

另请参阅将命令绑定到事件?

这篇关于如何向控件的事件添加新命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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