Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

74 rader
2.2 KiB

  1. using System.Windows;
  2. using System.Windows.Controls;
  3. namespace BPA.UIControl
  4. {
  5. /// <summary>
  6. /// 消息容器
  7. /// </summary>
  8. [TemplatePart(Name = MessageStackPanelName, Type = typeof(StackPanel))]
  9. public class MessageContainer : ContentControl
  10. {
  11. /// <summary>
  12. /// 消息堆叠面板名称
  13. /// </summary>
  14. public const string MessageStackPanelName = "PART_MessageStackPanel";
  15. private StackPanel messageStackPanel;
  16. static MessageContainer()
  17. {
  18. DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageContainer), new FrameworkPropertyMetadata(typeof(MessageContainer)));
  19. }
  20. /// <inheritdoc/>
  21. public override void OnApplyTemplate()
  22. {
  23. base.OnApplyTemplate();
  24. messageStackPanel = GetTemplateChild(MessageStackPanelName) as StackPanel;
  25. }
  26. /// <summary>
  27. /// 标识
  28. /// </summary>
  29. public static readonly DependencyProperty IdentifierProperty =
  30. DependencyProperty.Register("Identifier", typeof(string), typeof(MessageContainer), 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 MessageContainer container)
  42. {
  43. string identify = e.NewValue.ToString();
  44. Message.UpdateContainer(container, identify);
  45. }
  46. }
  47. /// <summary>
  48. /// 添加消息卡片
  49. /// </summary>
  50. /// <param name="messageCard">消息卡片</param>
  51. internal void AddMessageCard(MessageCard messageCard)
  52. {
  53. _ = messageStackPanel.Children.Add(messageCard);
  54. }
  55. /// <summary>
  56. /// 移除消息卡片
  57. /// </summary>
  58. /// <param name="messageCard"></param>
  59. internal void RemoveMessageCard(MessageCard messageCard)
  60. {
  61. messageStackPanel.Children.Remove(messageCard);
  62. }
  63. }
  64. }