DelegateCommand 是否与“附加行为"相同? [英] Is a DelegateCommand the same as an "Attached Behavior"?

查看:55
本文介绍了DelegateCommand 是否与“附加行为"相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 MVVM Visual Studio 模板 在 CodePlex.这非常好让视图能够在其视图模型上执行命令.

I have been using the DelegateCommand found in the MVVM Visual Studio template at CodePlex. This works very nicely for having Views be able to execute commands on their ViewModel.

我在某处读到在 MVVM 中应该使用附加行为".据我所知,附加行为"是与 DelegateCommand 相同类型的模式,但被 Silverlight 使用,因为它没有命令.

I read somewhere that in MVVM "attached behaviors" should be used. As far as I can tell, "attached behaviors" are the same type of pattern as DelegateCommand but are used by Silverlight since it does not have commands.

这是正确的吗?或者,除了 DelegateCommand 之外,附加行为"在种类上是否有所不同,值得学习?

Is this correct? Or are "attached behaviors" something different in kind and worth learning in addtiion to DelegateCommand?

推荐答案

A DelegateCommand(或 RelayCommand,几乎相同但接受一个参数)只是 ICommand 接口的轻量级实现,它允许 ViewModel轻松公开命令.

A DelegateCommand (or a RelayCommand, which is almost the same but accepts a parameter) is just a light-weight implementation of the ICommand interface that allows a ViewModel to expose commands easily.

附加行为是将事件链接到命令的一种方式.例如,大多数 WPF 控件没有 Command 属性,因此您通常无法定义命令来对其事件做出反应.通过附加行为,您可以将任何控件的任何事件绑定"到您的 ViewModel 命令.看看 Marlon Grech 的实现,这似乎对我来说最灵活

Attached behaviors are a way to link an event to a command. For instance, most WPF controls don't have a Command property, so you can't normally define a command to react to their events. With attached behaviors, you can "bind" any event of any control to a command of your ViewModel. Have a look at Marlon Grech's implementation, which seems the most flexible to me

这篇关于DelegateCommand 是否与“附加行为"相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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