You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.3 KiB

  1. using BPA.UIControl.Commons.KnownBoxes;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace BPA.UIControl
  17. {
  18. /// <summary>
  19. /// 标记
  20. /// </summary>
  21. public class Badge : ContentControl
  22. {
  23. static Badge()
  24. {
  25. DefaultStyleKeyProperty.OverrideMetadata(typeof(Badge), new FrameworkPropertyMetadata(typeof(Badge)));
  26. }
  27. #region 属性
  28. /// <summary>
  29. /// 标记颜色
  30. /// </summary>
  31. public static readonly DependencyProperty BadgeBrushProperty =
  32. DependencyProperty.Register("BadgeBrush", typeof(Brush), typeof(Badge), new PropertyMetadata(default(Brush)));
  33. /// <summary>
  34. /// 标记颜色
  35. /// </summary>
  36. public Brush BadgeBrush
  37. {
  38. get { return (Brush)GetValue(BadgeBrushProperty); }
  39. set { SetValue(BadgeBrushProperty, value); }
  40. }
  41. /// <summary>
  42. /// 显示文本
  43. /// </summary>
  44. public static readonly DependencyProperty TextProperty =
  45. DependencyProperty.Register("Text", typeof(string), typeof(Badge), new PropertyMetadata(default(string), OnTextChanged));
  46. /// <summary>
  47. /// 显示文本
  48. /// </summary>
  49. public string Text
  50. {
  51. get { return (string)GetValue(TextProperty); }
  52. set { SetValue(TextProperty, value); }
  53. }
  54. /// <summary>
  55. /// 标记尺寸
  56. /// </summary>
  57. public static readonly DependencyProperty BadgeSizeProperty =
  58. DependencyProperty.Register("BadgeSize", typeof(double), typeof(Badge), new PropertyMetadata(default(double)));
  59. /// <summary>
  60. /// 标记尺寸
  61. /// </summary>
  62. public double BadgeSize
  63. {
  64. get { return (double)GetValue(BadgeSizeProperty); }
  65. set { SetValue(BadgeSizeProperty, value); }
  66. }
  67. /// <summary>
  68. /// 标记字体尺寸
  69. /// </summary>
  70. public static readonly DependencyProperty BadgeFontSizeProperty =
  71. DependencyProperty.Register("BadgeFontSize", typeof(double), typeof(Badge), new PropertyMetadata(default(double)));
  72. /// <summary>
  73. /// 标记字体尺寸
  74. /// </summary>
  75. public double BadgeFontSize
  76. {
  77. get { return (double)GetValue(BadgeFontSizeProperty); }
  78. set { SetValue(BadgeFontSizeProperty, value); }
  79. }
  80. /// <summary>
  81. /// 是否隐藏
  82. /// </summary>
  83. public static readonly DependencyProperty IsHiddenProperty =
  84. DependencyProperty.Register("IsHidden", typeof(bool), typeof(Badge), new PropertyMetadata(BooleanBoxes.FalseBox));
  85. /// <summary>
  86. /// 是否隐藏
  87. /// </summary>
  88. public bool IsHidden
  89. {
  90. get { return (bool)GetValue(IsHiddenProperty); }
  91. set { SetValue(IsHiddenProperty, BooleanBoxes.Box(value)); }
  92. }
  93. #endregion 属性
  94. #region 事件
  95. /// <summary>
  96. /// 文本改变事件
  97. /// </summary>
  98. public static readonly RoutedEvent TextChangedEvent =
  99. EventManager.RegisterRoutedEvent("TextChanged", RoutingStrategy.Direct, typeof(RoutedEventArgs), typeof(Badge));
  100. /// <summary>
  101. /// 文本改变事件
  102. /// </summary>处理
  103. public event RoutedEventHandler TextChanged
  104. {
  105. add { AddHandler(TextChangedEvent, value); }
  106. remove { RemoveHandler(TextChangedEvent, value); }
  107. }
  108. #endregion 事件
  109. #region 方法
  110. private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  111. {
  112. if (e.OldValue == null)
  113. {
  114. return;
  115. }
  116. Badge badge = d as Badge;
  117. RoutedEventArgs args = new RoutedEventArgs();
  118. args.RoutedEvent = TextChangedEvent;
  119. badge.RaiseEvent(args);
  120. }
  121. #endregion 方法
  122. }
  123. }