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.

147 lines
5.0 KiB

  1. using BPA.UIControl.Commons.KnownBoxes;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. using System.Windows.Media;
  8. namespace BPA.UIControl
  9. {
  10. /// <summary>
  11. /// 消息框容器
  12. /// </summary>
  13. [TemplatePart(Name = TransitionPartName, Type = typeof(Transition))]
  14. public class MessageBoxContainer : ContentControl
  15. {
  16. const string TransitionPartName = "Path_Transition";
  17. private List<FrameworkElement> focusableElements; // Content 内 focusable 元素,用于打开弹窗使其失效
  18. static MessageBoxContainer()
  19. {
  20. DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageBoxContainer), new FrameworkPropertyMetadata(typeof(MessageBoxContainer)));
  21. }
  22. /// <inheritdoc/>
  23. public override void OnApplyTemplate()
  24. {
  25. base.OnApplyTemplate();
  26. if (GetTemplateChild(TransitionPartName) is Transition transition)
  27. {
  28. transition.Showed += (sender, e) => IsClosed = false;
  29. transition.Closed += (sender, e) => IsClosed = true;
  30. }
  31. focusableElements = new List<FrameworkElement>();
  32. }
  33. /// <summary>
  34. /// 标识
  35. /// </summary>
  36. public static readonly DependencyProperty IdentifierProperty =
  37. DependencyProperty.Register("Identifier", typeof(string), typeof(MessageBoxContainer), new PropertyMetadata(default(string), OnIdentifierChanged));
  38. /// <summary>
  39. /// 标识
  40. /// </summary>
  41. public string Identifier
  42. {
  43. get { return (string)GetValue(IdentifierProperty); }
  44. set { SetValue(IdentifierProperty, value); }
  45. }
  46. /// <summary>
  47. /// 弹窗内容
  48. /// </summary>
  49. public static readonly DependencyProperty DialogContentProperty =
  50. DependencyProperty.Register("DialogContent", typeof(object), typeof(MessageBoxContainer), new PropertyMetadata(default(object)));
  51. /// <summary>
  52. /// 弹窗内容
  53. /// </summary>
  54. public object DialogContent
  55. {
  56. get { return GetValue(DialogContentProperty); }
  57. set { SetValue(DialogContentProperty, value); }
  58. }
  59. private static void OnIdentifierChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  60. {
  61. if (d is MessageBoxContainer container)
  62. {
  63. string identify = e.NewValue.ToString();
  64. MessageBoxR.UpdateContainer(container, identify);
  65. }
  66. }
  67. /// <summary>
  68. /// 遮罩背景色
  69. /// </summary>
  70. public static readonly DependencyProperty MaskBackgroundProperty = DependencyProperty.Register(
  71. "MaskBackground", typeof(Brush), typeof(MessageBoxContainer), new PropertyMetadata(default(Brush)));
  72. /// <summary>
  73. /// 遮罩背景色
  74. /// </summary>
  75. public Brush MaskBackground
  76. {
  77. get { return (Brush)GetValue(MaskBackgroundProperty); }
  78. set { SetValue(MaskBackgroundProperty, value); }
  79. }
  80. /// <summary>
  81. /// 是否显示
  82. /// </summary>
  83. public static readonly DependencyProperty IsShowProperty =
  84. DependencyProperty.Register("IsShow", typeof(bool), typeof(MessageBoxContainer), new PropertyMetadata(BooleanBoxes.FalseBox, OnIsShowChanged));
  85. private static void OnIsShowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  86. {
  87. var container = d as MessageBoxContainer;
  88. if (container.IsShow)
  89. {
  90. var dialogContent = container.Content as FrameworkElement;
  91. dialogContent.ForEachVisualChild(x =>
  92. {
  93. if (x is FrameworkElement element && element.Focusable)
  94. {
  95. element.Focusable = false;
  96. container.focusableElements.Add(element);
  97. }
  98. });
  99. }
  100. else
  101. {
  102. container.focusableElements.ForEach(x => x.Focusable = true);
  103. container.focusableElements.Clear();
  104. }
  105. }
  106. /// <summary>
  107. /// 是否显示
  108. /// </summary>
  109. public bool IsShow
  110. {
  111. get { return (bool)GetValue(IsShowProperty); }
  112. set { SetValue(IsShowProperty, BooleanBoxes.Box(value)); }
  113. }
  114. /// <summary>
  115. /// 是否关闭后
  116. /// </summary>
  117. public static readonly DependencyProperty IsClosedProperty =
  118. DependencyProperty.Register("IsClosed", typeof(bool), typeof(MessageBoxContainer), new PropertyMetadata(BooleanBoxes.FalseBox));
  119. /// <summary>
  120. /// 是否关闭后
  121. /// </summary>
  122. public bool IsClosed
  123. {
  124. get { return (bool)GetValue(IsClosedProperty); }
  125. set { SetValue(IsClosedProperty, BooleanBoxes.Box(value)); }
  126. }
  127. }
  128. }