25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.4 KiB

  1. using System.Windows.Input;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. namespace BPA.UIControl
  5. {
  6. /// <summary>
  7. /// 双击转命令
  8. /// </summary>
  9. public class DoubleClicker : ContentControl
  10. {
  11. /// <summary>
  12. /// 命令
  13. /// </summary>
  14. public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
  15. "CommandParameter", typeof(object), typeof(DoubleClicker), new FrameworkPropertyMetadata(null));
  16. /// <summary>
  17. /// 命令参数
  18. /// </summary>
  19. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  20. "Command", typeof(ICommand), typeof(DoubleClicker), new FrameworkPropertyMetadata(null));
  21. /// <summary>
  22. /// 是否是双击区域
  23. /// </summary>
  24. public static readonly DependencyProperty IsDoubleClickAreaProperty = DependencyProperty.RegisterAttached(
  25. "IsDoubleClickArea", typeof(bool), typeof(DoubleClicker), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits));
  26. /// <summary>
  27. /// 命令
  28. /// </summary>
  29. public ICommand Command
  30. {
  31. get => (ICommand)GetValue(CommandProperty);
  32. set => SetValue(CommandProperty, value);
  33. }
  34. /// <summary>
  35. /// 命令参数
  36. /// </summary>
  37. public object CommandParameter
  38. {
  39. get => GetValue(CommandParameterProperty);
  40. set => SetValue(CommandParameterProperty, value);
  41. }
  42. /// <summary>
  43. /// 是否是双击区域
  44. /// </summary>
  45. public static void SetIsDoubleClickArea(DependencyObject element, bool value)
  46. {
  47. element.SetValue(IsDoubleClickAreaProperty, value);
  48. }
  49. /// <summary>
  50. /// 是否是双击区域
  51. /// </summary>
  52. public static bool GetIsDoubleClickArea(DependencyObject element)
  53. {
  54. return (bool)element.GetValue(IsDoubleClickAreaProperty);
  55. }
  56. /// <inheritdoc/>
  57. protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
  58. {
  59. base.OnMouseDoubleClick(e);
  60. if (Command != null && GetIsDoubleClickArea((DependencyObject)e.OriginalSource))
  61. {
  62. Command.Execute(CommandParameter);
  63. }
  64. }
  65. }
  66. }