SelectableItem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Input;
  4. using HandyControl.Data;
  5. namespace HandyControl.Controls;
  6. public class SelectableItem : ContentControl, ISelectable
  7. {
  8. private bool _isMouseLeftButtonDown;
  9. protected override void OnMouseLeave(MouseEventArgs e)
  10. {
  11. base.OnMouseLeave(e);
  12. _isMouseLeftButtonDown = false;
  13. }
  14. protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
  15. {
  16. base.OnMouseLeftButtonDown(e);
  17. _isMouseLeftButtonDown = true;
  18. }
  19. protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
  20. {
  21. base.OnMouseLeftButtonUp(e);
  22. if (_isMouseLeftButtonDown)
  23. {
  24. if (SelfManage)
  25. {
  26. if (!IsSelected)
  27. {
  28. IsSelected = true;
  29. OnSelected(new RoutedEventArgs(SelectedEvent, this));
  30. }
  31. else if (CanDeselect)
  32. {
  33. IsSelected = false;
  34. OnSelected(new RoutedEventArgs(DeselectedEvent, this));
  35. }
  36. }
  37. else
  38. {
  39. if (CanDeselect)
  40. {
  41. OnSelected(IsSelected
  42. ? new RoutedEventArgs(DeselectedEvent, this)
  43. : new RoutedEventArgs(SelectedEvent, this));
  44. }
  45. else
  46. {
  47. OnSelected(new RoutedEventArgs(SelectedEvent, this));
  48. }
  49. }
  50. _isMouseLeftButtonDown = false;
  51. }
  52. }
  53. protected virtual void OnSelected(RoutedEventArgs e) => RaiseEvent(e);
  54. public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register(
  55. nameof(IsSelected), typeof(bool), typeof(SelectableItem), new PropertyMetadata(ValueBoxes.FalseBox));
  56. public bool IsSelected
  57. {
  58. get => (bool) GetValue(IsSelectedProperty);
  59. set => SetValue(IsSelectedProperty, ValueBoxes.BooleanBox(value));
  60. }
  61. public static readonly DependencyProperty SelfManageProperty = DependencyProperty.Register(
  62. nameof(SelfManage), typeof(bool), typeof(SelectableItem), new PropertyMetadata(ValueBoxes.FalseBox));
  63. public bool SelfManage
  64. {
  65. get => (bool) GetValue(SelfManageProperty);
  66. set => SetValue(SelfManageProperty, ValueBoxes.BooleanBox(value));
  67. }
  68. public static readonly DependencyProperty CanDeselectProperty = DependencyProperty.Register(
  69. nameof(CanDeselect), typeof(bool), typeof(SelectableItem), new PropertyMetadata(ValueBoxes.FalseBox));
  70. public bool CanDeselect
  71. {
  72. get => (bool) GetValue(CanDeselectProperty);
  73. set => SetValue(CanDeselectProperty, ValueBoxes.BooleanBox(value));
  74. }
  75. public static readonly RoutedEvent SelectedEvent =
  76. EventManager.RegisterRoutedEvent("Selected", RoutingStrategy.Bubble,
  77. typeof(RoutedEventHandler), typeof(SelectableItem));
  78. public event RoutedEventHandler Selected
  79. {
  80. add => AddHandler(SelectedEvent, value);
  81. remove => RemoveHandler(SelectedEvent, value);
  82. }
  83. public static readonly RoutedEvent DeselectedEvent =
  84. EventManager.RegisterRoutedEvent("Deselected", RoutingStrategy.Bubble,
  85. typeof(RoutedEventHandler), typeof(SelectableItem));
  86. public event RoutedEventHandler Deselected
  87. {
  88. add => AddHandler(DeselectedEvent, value);
  89. remove => RemoveHandler(DeselectedEvent, value);
  90. }
  91. }