MVVM 的事件处理: 利用Prism框架进行EventToCommand
2011-07-29 17:52
513 查看
Prism框架只对Button提供了Command的附加属性,虽然通过ControlTemplate可以实现大部分的功能,但是总是重写ControlTemplate未免费时费力,而且重写的ControlTemplate还不一定有原来的动态效果,这里提供一个解决方案。
此处对ComboBox的SelectionChanged事件做EventToCommand。
这里用到Prism中的CommandBehaviorBase类。
1. 定义附加属性类SelectedChanged.cs
2. 定义Behavior类
OK!接下来在ComboBox就可以使用这个附加属性了
Lcommands:SelectedChanged.Command="{Binding FamilySelectCommand}"
Lcommands:SelectedChanged.CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource Self}}"/>
此处对ComboBox的SelectionChanged事件做EventToCommand。
这里用到Prism中的CommandBehaviorBase类。
1. 定义附加属性类SelectedChanged.cs
public class SelectedChanged { private static readonly DependencyProperty SelectedChangedCommandBehaviorProperty = DependencyProperty.RegisterAttached( "SelectedChangedCommandBehavior", typeof(ComboBoxSelectChangedCommandBehavior), typeof(SelectedChanged), null); public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached( "Command", typeof(ICommand), typeof(SelectedChanged), new PropertyMetadata(OnSetCommandCallback)); public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached( "CommandParameter", typeof(object), typeof(SelectedChanged), new PropertyMetadata(OnSetCommandParameterCallback)); public static void SetCommand(ComboBox CBox, ICommand command) { if (CBox == null) throw new System.ArgumentNullException("CBox"); CBox.SetValue(CommandProperty, command); } public static ICommand GetCommand(ComboBox CBox) { if (CBox == null) throw new System.ArgumentNullException("CBox"); return CBox.GetValue(CommandProperty) as ICommand; } public static void SetCommandParameter(ComboBox CBox, object parameter) { if (CBox == null) throw new System.ArgumentNullException("CBox"); CBox.SetValue(CommandParameterProperty, parameter); } public static object GetCommandParameter(ComboBox CBox) { if (CBox == null) throw new System.ArgumentNullException("CBox"); return CBox.GetValue(CommandParameterProperty); } private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { ComboBox CBox = dependencyObject as ComboBox; if (CBox != null) { ComboBoxSelectChangedCommandBehavior behavior = GetOrCreateBehavior(CBox); behavior.Command = e.NewValue as ICommand; } } private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { ComboBox CBox = dependencyObject as ComboBox; if (CBox != null) { ComboBoxSelectChangedCommandBehavior behavior = GetOrCreateBehavior(CBox); behavior.CommandParameter = e.NewValue; } } private static ComboBoxSelectChangedCommandBehavior GetOrCreateBehavior(ComboBox CBox) { ComboBoxSelectChangedCommandBehavior behavior = CBox.GetValue(SelectedChangedCommandBehaviorProperty) as ComboBoxSelectChangedCommandBehavior; if (behavior == null) { behavior = new ComboBoxSelectChangedCommandBehavior(CBox); CBox.SetValue(SelectedChangedCommandBehaviorProperty, behavior); } return behavior; } }
2. 定义Behavior类
public ComboBoxSelectChangedCommandBehavior(ComboBox CMBObject) : base(CMBObject) { if (CMBObject == null) throw new System.ArgumentNullException("CMBObject can't be null"); CMBObject.SelectionChanged += (s, e) => { ExecuteCommand(); }; }
OK!接下来在ComboBox就可以使用这个附加属性了
Lcommands:SelectedChanged.Command="{Binding FamilySelectCommand}"
Lcommands:SelectedChanged.CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource Self}}"/>
相关文章推荐
- 利用jQuery的$.event.fix函数统一浏览器event事件处理
- Android — 长按ListView 利用上下文菜单(ActionMode) 进行批量事件处理
- WPF Prism框架下基于MVVM模式的命令、绑定、事件
- 利用jQuery的$.event.fix函数统一浏览器event事件处理的差异
- Linq To Sql模式中自动生成T-SQL增删改操作系列~PropertyChanged事件实现在子类中记录属性的变化,在基类中进行统一处理
- 利用jQuery的$.event.fix函数统一浏览器event事件处理
- 利用Tstringlist对文件流进行处理(介绍extractfilepath(),savetofile(),loadfromfile()使用)
- MVVM之旅-给任意的事件绑定命令(Adventures in MVVM – Binding Commands to ANY Event) .
- spring容器加载完后事件处理(利用ContextRefreshedEvent事件)
- MVVM之旅-给任意的事件绑定命令(Adventures in MVVM – Binding Commands to ANY Event)
- 事件处理框架-event_base
- 利用move.js框架进行边旋转边扩大边移动处理案例
- 利用勾子监视系统或进程中的各种事件消息,截获发往目标窗口的消息并进行处理
- 利用jQuery的$.event.fix函数统一浏览器event事件处理
- 利用Tstringlist对文件流进行处理(介绍extractfilepath(),savetofile(),loadfromfile()使用)
- Windwos Phone MVVM Light的 EventToCommand 命令绑定实现方式
- 利用jQuery的$.event.fix函数统一浏览器event事件处理
- Libevent之事件处理框架-event_base结构体学习
- Android界面之----自定义的Dialog,然后利用回调方法,在调用处,进行Dialog中各按钮的事件处理。