Badge.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. using HandyControl.Data;
  6. namespace HandyControl.Controls;
  7. /// <summary>
  8. /// 标记
  9. /// </summary>
  10. public class Badge : ContentControl
  11. {
  12. private int? _originalValue;
  13. public static readonly RoutedEvent ValueChangedEvent =
  14. EventManager.RegisterRoutedEvent("ValueChanged", RoutingStrategy.Bubble,
  15. typeof(EventHandler<FunctionEventArgs<int>>), typeof(Badge));
  16. public event EventHandler<FunctionEventArgs<int>> ValueChanged
  17. {
  18. add => AddHandler(ValueChangedEvent, value);
  19. remove => RemoveHandler(ValueChangedEvent, value);
  20. }
  21. public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
  22. nameof(Text), typeof(string), typeof(Badge), new PropertyMetadata("0"));
  23. public string Text
  24. {
  25. get => (string) GetValue(TextProperty);
  26. set => SetValue(TextProperty, value);
  27. }
  28. public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
  29. nameof(Value), typeof(int), typeof(Badge), new PropertyMetadata(ValueBoxes.Int0Box, OnValueChanged));
  30. private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  31. {
  32. var ctl = (Badge) d;
  33. var v = (int) e.NewValue;
  34. ctl.SetCurrentValue(TextProperty, v <= ctl.Maximum ? v.ToString() : $"{ctl.Maximum}+");
  35. if (ctl.IsLoaded)
  36. {
  37. ctl.RaiseEvent(new FunctionEventArgs<int>(ValueChangedEvent, ctl)
  38. {
  39. Info = v
  40. });
  41. }
  42. else
  43. {
  44. ctl._originalValue = v;
  45. }
  46. }
  47. public int Value
  48. {
  49. get => (int) GetValue(ValueProperty);
  50. set => SetValue(ValueProperty, value);
  51. }
  52. public static readonly DependencyProperty StatusProperty = DependencyProperty.Register(
  53. nameof(Status), typeof(BadgeStatus), typeof(Badge), new PropertyMetadata(default(BadgeStatus)));
  54. public BadgeStatus Status
  55. {
  56. get => (BadgeStatus) GetValue(StatusProperty);
  57. set => SetValue(StatusProperty, value);
  58. }
  59. public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register(
  60. nameof(Maximum), typeof(int), typeof(Badge), new PropertyMetadata(ValueBoxes.Int99Box, OnMaximumChanged));
  61. private static void OnMaximumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  62. {
  63. var ctl = (Badge) d;
  64. var v = ctl.Value;
  65. ctl.SetCurrentValue(TextProperty, v <= ctl.Maximum ? v.ToString() : $"{ctl.Maximum}+");
  66. }
  67. public int Maximum
  68. {
  69. get => (int) GetValue(MaximumProperty);
  70. set => SetValue(MaximumProperty, value);
  71. }
  72. public static readonly DependencyProperty BadgeMarginProperty = DependencyProperty.Register(
  73. nameof(BadgeMargin), typeof(Thickness), typeof(Badge), new PropertyMetadata(default(Thickness)));
  74. public Thickness BadgeMargin
  75. {
  76. get => (Thickness) GetValue(BadgeMarginProperty);
  77. set => SetValue(BadgeMarginProperty, value);
  78. }
  79. public static readonly DependencyProperty ShowBadgeProperty = DependencyProperty.Register(
  80. nameof(ShowBadge), typeof(bool), typeof(Badge), new PropertyMetadata(ValueBoxes.TrueBox));
  81. public bool ShowBadge
  82. {
  83. get => (bool) GetValue(ShowBadgeProperty);
  84. set => SetValue(ShowBadgeProperty, ValueBoxes.BooleanBox(value));
  85. }
  86. protected override Geometry GetLayoutClip(Size layoutSlotSize) => null;
  87. public override void OnApplyTemplate()
  88. {
  89. base.OnApplyTemplate();
  90. if (_originalValue != null)
  91. {
  92. RaiseEvent(new FunctionEventArgs<int>(ValueChangedEvent, this)
  93. {
  94. Info = _originalValue.Value
  95. });
  96. _originalValue = null;
  97. }
  98. }
  99. }