Silverlight - Attached Behavior + Command

CAL (Prism)中的Command绑定非常好用,可以将某个button的click事件绑定到ViewModel中的某个方法上,简单明了。但是,

 

CAL (Prism)中的Commands对Silverlight的支持有一个限制 ,只实现了对ButtonBase的Click事件的Command Binding:


The command support in Silverlight is built using an attached behavior pattern. This pattern connects events raised by controls to code on a Presenter or PresentationModel. In WPF and Silverlight, an attached behavior is comprised of two parts: an attached property and a behavior object. The attached property establishes a relationship between the target control and the behavior object. The behavior object monitors the target control and takes action based on events or state change in the control. For example, the Composite Application Library provides an attached behavior that executes commands from the Click event of ButtonBase.

 

对其他控件中的事件则需要自己来实现:

 

Frequently, applications need to invoke commands from controls or events other than the Click event from ButtonBase . In these cases, you need to define your own attached property and behavior. The Composite Application Library provides a CommandBehaviorBase <T> to make it easier to create behaviors that interact with ICommands . This class invokes the command and monitors changes in the command's CanExecuteChanged event, and can be used to extend command support in both Silverlight and WPF.

To create your custom behavior, create a class that inherits from CommandBehaviorBase <T> and targets the control you want to monitor. In the constructor of your class, you will need to subscribe to the events you want to monitor from the control.

 

这个实现其是Attached Behavior 与 CAL (Prism)中Command的一个结合。这里有一个例子

 

关于Attached Behavior

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值