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.

171 lines
5.5 KiB

  1. using System.Windows.Controls.Primitives;
  2. using System.Windows.Controls;
  3. using System.Windows.Media;
  4. using System.Windows;
  5. using BPA.UIControl.Commons.KnownBoxes;
  6. namespace BPA.UIControl
  7. {
  8. /// <summary>
  9. /// 通知卡片
  10. /// </summary>
  11. [TemplatePart(Name = TransitionPartName, Type = typeof(Transition))]
  12. [TemplatePart(Name = CloseButtonName, Type = typeof(Button))]
  13. public class NotificationCard : ContentControl
  14. {
  15. /// <summary>
  16. /// 转换动画名称
  17. /// </summary>
  18. public const string TransitionPartName = "Path_Transition";
  19. /// <summary>
  20. /// 关闭按钮名称
  21. /// </summary>
  22. public const string CloseButtonName = "PART_CloseButton";
  23. static NotificationCard()
  24. {
  25. DefaultStyleKeyProperty.OverrideMetadata(typeof(NotificationCard), new FrameworkPropertyMetadata(typeof(NotificationCard)));
  26. }
  27. /// <inheritdoc/>
  28. public override void OnApplyTemplate()
  29. {
  30. base.OnApplyTemplate();
  31. if (GetTemplateChild(CloseButtonName) is ButtonBase closeButton)
  32. {
  33. closeButton.Click += (sender, e) =>
  34. {
  35. if (!IsShow)
  36. {
  37. IsShow = true;
  38. }
  39. IsShow = false;
  40. };
  41. }
  42. if (GetTemplateChild(TransitionPartName) is Transition transition)
  43. {
  44. transition.Closed += (sender, e) =>
  45. {
  46. RoutedEventArgs eventArgs = new RoutedEventArgs(CloseEvent, this);
  47. this.RaiseEvent(eventArgs);
  48. };
  49. }
  50. }
  51. #region 事件
  52. /// <summary>
  53. /// 关闭消息事件
  54. /// </summary>
  55. public static readonly RoutedEvent CloseEvent = EventManager.RegisterRoutedEvent("Close", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(NotificationCard));
  56. /// <summary>
  57. /// 关闭消息事件
  58. /// </summary>
  59. public event RoutedEventHandler Close
  60. {
  61. add { AddHandler(CloseEvent, value); }
  62. remove { RemoveHandler(CloseEvent, value); }
  63. }
  64. #endregion 事件
  65. #region 依赖属性
  66. /// <summary>
  67. /// 圆角半径
  68. /// </summary>
  69. public static readonly DependencyProperty CornerRadiusProperty =
  70. DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(NotificationCard), new PropertyMetadata(default(CornerRadius)));
  71. /// <summary>
  72. /// 圆角半径
  73. /// </summary>
  74. public CornerRadius CornerRadius
  75. {
  76. get { return (CornerRadius)GetValue(CornerRadiusProperty); }
  77. set { SetValue(CornerRadiusProperty, value); }
  78. }
  79. /// <summary>
  80. /// 主题色
  81. /// </summary>
  82. public static readonly DependencyProperty ThemeColorBrushProperty =
  83. DependencyProperty.Register("ThemeColorBrush", typeof(SolidColorBrush), typeof(NotificationCard), new PropertyMetadata(default(SolidColorBrush)));
  84. /// <summary>
  85. /// 主题色
  86. /// </summary>
  87. public SolidColorBrush ThemeColorBrush
  88. {
  89. get { return (SolidColorBrush)GetValue(ThemeColorBrushProperty); }
  90. set { SetValue(ThemeColorBrushProperty, value); }
  91. }
  92. /// <summary>
  93. /// 通知类型
  94. /// </summary>
  95. public static readonly DependencyProperty TypeProperty =
  96. DependencyProperty.Register("Type", typeof(NotificationType), typeof(NotificationCard), new PropertyMetadata(default(NotificationType)));
  97. /// <summary>
  98. /// 通知类型
  99. /// </summary>
  100. public NotificationType Type
  101. {
  102. get { return (NotificationType)GetValue(TypeProperty); }
  103. set { SetValue(TypeProperty, value); }
  104. }
  105. /// <summary>
  106. /// 是否可清除
  107. /// </summary>
  108. public static readonly DependencyProperty IsClearableProperty =
  109. DependencyProperty.Register("IsClearable", typeof(bool), typeof(NotificationCard), new PropertyMetadata(BooleanBoxes.FalseBox));
  110. /// <summary>
  111. /// 是否可清除
  112. /// </summary>
  113. public bool IsClearable
  114. {
  115. get { return (bool)GetValue(IsClearableProperty); }
  116. set { SetValue(IsClearableProperty, BooleanBoxes.Box(value)); }
  117. }
  118. /// <summary>
  119. /// 是否显示
  120. /// </summary>
  121. public static readonly DependencyProperty IsShowProperty =
  122. DependencyProperty.Register("IsShow", typeof(bool), typeof(NotificationCard), new PropertyMetadata(BooleanBoxes.FalseBox));
  123. /// <summary>
  124. /// 是否显示
  125. /// </summary>
  126. public bool IsShow
  127. {
  128. get { return (bool)GetValue(IsShowProperty); }
  129. set { SetValue(IsShowProperty, BooleanBoxes.Box(value)); }
  130. }
  131. /// <summary>
  132. /// 标题
  133. /// </summary>
  134. public static readonly DependencyProperty TitleProperty =
  135. DependencyProperty.Register("Title", typeof(string), typeof(NotificationCard), new PropertyMetadata(default(string)));
  136. /// <summary>
  137. /// 标题
  138. /// </summary>
  139. public string Title
  140. {
  141. get { return (string)GetValue(TitleProperty); }
  142. set { SetValue(TitleProperty, value); }
  143. }
  144. #endregion 依赖属性
  145. }
  146. }