SplitButton.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Controls.Primitives;
  4. using System.Windows.Input;
  5. using HandyControl.Data;
  6. using HandyControl.Data.Enum;
  7. namespace HandyControl.Controls;
  8. public class SplitButton : ButtonBase
  9. {
  10. public static readonly DependencyProperty HitModeProperty = DependencyProperty.Register(
  11. nameof(HitMode), typeof(HitMode), typeof(SplitButton), new PropertyMetadata(default(HitMode)));
  12. public HitMode HitMode
  13. {
  14. get => (HitMode) GetValue(HitModeProperty);
  15. set => SetValue(HitModeProperty, value);
  16. }
  17. public static readonly DependencyProperty MaxDropDownHeightProperty = DependencyProperty.Register(
  18. nameof(MaxDropDownHeight), typeof(double), typeof(SplitButton), new PropertyMetadata(SystemParameters.PrimaryScreenHeight / 3.0));
  19. public double MaxDropDownHeight
  20. {
  21. get => (double) GetValue(MaxDropDownHeightProperty);
  22. set => SetValue(MaxDropDownHeightProperty, value);
  23. }
  24. public static readonly DependencyProperty IsDropDownOpenProperty = DependencyProperty.Register(
  25. nameof(IsDropDownOpen), typeof(bool), typeof(SplitButton), new PropertyMetadata(ValueBoxes.FalseBox));
  26. public bool IsDropDownOpen
  27. {
  28. get => (bool) GetValue(IsDropDownOpenProperty);
  29. set => SetValue(IsDropDownOpenProperty, ValueBoxes.BooleanBox(value));
  30. }
  31. public static readonly DependencyProperty DropDownContentProperty = DependencyProperty.Register(
  32. nameof(DropDownContent), typeof(object), typeof(SplitButton), new PropertyMetadata(default(object)));
  33. public object DropDownContent
  34. {
  35. get => GetValue(DropDownContentProperty);
  36. set => SetValue(DropDownContentProperty, value);
  37. }
  38. public SplitButton()
  39. {
  40. AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(ItemsOnClick));
  41. }
  42. private void ItemsOnClick(object sender, RoutedEventArgs e)
  43. {
  44. if (e.OriginalSource is MenuItem)
  45. {
  46. SetCurrentValue(IsDropDownOpenProperty, ValueBoxes.FalseBox);
  47. }
  48. }
  49. protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
  50. {
  51. base.OnPreviewMouseLeftButtonDown(e);
  52. if (HitMode == HitMode.Hover)
  53. {
  54. e.Handled = true;
  55. }
  56. }
  57. protected override void OnMouseEnter(MouseEventArgs e)
  58. {
  59. base.OnMouseEnter(e);
  60. if (HitMode == HitMode.Hover)
  61. {
  62. SetCurrentValue(IsDropDownOpenProperty, ValueBoxes.TrueBox);
  63. }
  64. }
  65. }