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.

74 lines
2.2 KiB

  1. using System.Windows.Controls;
  2. using System.Windows;
  3. namespace BPA.UIControl
  4. {
  5. /// <summary>
  6. /// 消通知容器
  7. /// </summary>
  8. [TemplatePart(Name = StackPanelName, Type = typeof(StackPanel))]
  9. public class NotificationContainer : ContentControl
  10. {
  11. /// <summary>
  12. /// 消息堆叠面板名称
  13. /// </summary>
  14. public const string StackPanelName = "PART_StackPanel";
  15. private StackPanel stackPanel;
  16. static NotificationContainer()
  17. {
  18. DefaultStyleKeyProperty.OverrideMetadata(typeof(NotificationContainer), new FrameworkPropertyMetadata(typeof(NotificationContainer)));
  19. }
  20. /// <inheritdoc/>
  21. public override void OnApplyTemplate()
  22. {
  23. base.OnApplyTemplate();
  24. stackPanel = GetTemplateChild(StackPanelName) as StackPanel;
  25. }
  26. /// <summary>
  27. /// 标识
  28. /// </summary>
  29. public static readonly DependencyProperty IdentifierProperty =
  30. DependencyProperty.Register("Identifier", typeof(string), typeof(NotificationContainer), new PropertyMetadata(default(string), OnIdentifierChanged));
  31. /// <summary>
  32. /// 标识
  33. /// </summary>
  34. public string Identifier
  35. {
  36. get { return (string)GetValue(IdentifierProperty); }
  37. set { SetValue(IdentifierProperty, value); }
  38. }
  39. private static void OnIdentifierChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  40. {
  41. if (d is NotificationContainer container)
  42. {
  43. string identify = e.NewValue.ToString();
  44. Notification.UpdateContainer(container, identify);
  45. }
  46. }
  47. /// <summary>
  48. /// 添加卡片
  49. /// </summary>
  50. /// <param name="card">消息卡片</param>
  51. internal void AddCard(NotificationCard card)
  52. {
  53. stackPanel.Children.Insert(0, card);
  54. }
  55. /// <summary>
  56. /// 移除卡片
  57. /// </summary>
  58. /// <param name="card"></param>
  59. internal void RemoveCard(NotificationCard card)
  60. {
  61. stackPanel.Children.Remove(card);
  62. }
  63. }
  64. }