整理:WPF中应用附加事件制作可以绑定命令的其他事件

原文:整理:WPF中应用附加事件制作可以绑定命令的其他事件

目的:应用附加事件的方式定义可以绑定的事件,如MouseLeftButton、MouseDouble等等

一、定义属于Control的附加事件ControlAttachEvent类


  1. /// <summary> 附加事件 </summary>
  2. public static class ControlAttachEvent
  3. {
  4. #region - 双击事件 -
  5. public static readonly DependencyProperty PreviewMouseDoubleClickProperty =
  6. DependencyProperty.RegisterAttached("PreviewMouseDoubleClick", typeof(ICommand), typeof(ControlAttachEvent), new FrameworkPropertyMetadata(OnCommandChanged));
  7. public static ICommand GetPreviewMouseDoubleClick(Control target)
  8. {
  9. return (ICommand)target.GetValue(PreviewMouseDoubleClickProperty);
  10. }
  11. public static void SetPreviewMouseDoubleClick(Control target, ICommand value)
  12. {
  13. target.SetValue(PreviewMouseDoubleClickProperty, value);
  14. }
  15. private static void Element_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  16. {
  17. Control control = sender as Control;
  18. ICommand command = GetPreviewMouseDoubleClick(control);
  19. if (command.CanExecute(sender))
  20. {
  21. command.Execute(sender);
  22. e.Handled = true;
  23. }
  24. }
  25. private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  26. {
  27. Control control = d as Control;
  28. control.PreviewMouseDoubleClick += new MouseButtonEventHandler(Element_PreviewMouseDoubleClick);
  29. }
  30. #endregion
  31. public static DependencyProperty PreviewMouseLeftButtonDownCommandProperty = DependencyProperty.RegisterAttached("PreviewMouseLeftButtonDown",typeof(ICommand),typeof(ControlAttachEvent),new FrameworkPropertyMetadata(null, new PropertyChangedCallback(PreviewMouseLeftButtonDownChanged)));
  32. public static void SetPreviewMouseLeftButtonDown(DependencyObject target, ICommand value)
  33. {
  34. target.SetValue(PreviewMouseLeftButtonDownCommandProperty, value);
  35. }
  36. public static ICommand GetPreviewMouseLeftButtonDown(DependencyObject target)
  37. {
  38. return (ICommand)target.GetValue(PreviewMouseLeftButtonDownCommandProperty);
  39. }
  40. private static void PreviewMouseLeftButtonDownChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
  41. {
  42. FrameworkElement element = target as FrameworkElement;
  43. if (element != null)
  44. {
  45. if ((e.NewValue != null) && (e.OldValue == null))
  46. {
  47. element.PreviewMouseLeftButtonDown += element_PreviewMouseLeftButtonDown;
  48. }
  49. else if ((e.NewValue == null) && (e.OldValue != null))
  50. {
  51. element.PreviewMouseLeftButtonDown -= element_PreviewMouseLeftButtonDown;
  52. }
  53. }
  54. }
  55. private static void element_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  56. {
  57. FrameworkElement element = (FrameworkElement)sender;
  58. ICommand command = (ICommand)element.GetValue(PreviewMouseLeftButtonDownCommandProperty);
  59. command.Execute(sender);
  60. }
  61. }

说明:当控件MyControl中应用该项附加事件,如注册PreviewMouseDoubleClick事件,则会触发更新方法OnCommandChanged,而在更新方法中则会注册该控件MyControl的双击事件绑定的命令,由此实现双击该控件触发绑定命令的功能;

二、调用方法

  <Button  base:ControlAttachEvent.PreviewMouseDoubleClick="{Binding RelayCommand}"/>

如上代码当双击Button时会触发ViewModel中绑定的RelayCommand命令

注:

优点在于可以把控件中不支持绑定的事件支持绑定,同时应用附加属性复用性更强

缺点在于传递事件的参数只有sender,agrs需要特殊处理,同时支持的事件与Control有关,其他更上层的控件需要单独定义

上一篇:WPF中DATAGRID自定义验证(包含BINDINGGROUP)


下一篇:在WPF中一种较好的绑定Enums数据方法