PopupWindow.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. using HandyControl.Data;
  7. using HandyControl.Interactivity;
  8. using HandyControl.Tools;
  9. namespace HandyControl.Controls;
  10. /// <summary>
  11. /// 弹出窗口
  12. /// </summary>
  13. [TemplatePart(Name = ElementMainBorder, Type = typeof(Border))]
  14. [TemplatePart(Name = ElementTitleBlock, Type = typeof(TextBlock))]
  15. public class PopupWindow : System.Windows.Window
  16. {
  17. #region Constants
  18. private const string ElementMainBorder = "PART_MainBorder";
  19. private const string ElementTitleBlock = "PART_TitleBlock";
  20. #endregion Constants
  21. private Border _mainBorder;
  22. private TextBlock _titleBlock;
  23. private bool _showBackground = true;
  24. private FrameworkElement _targetElement;
  25. public override void OnApplyTemplate()
  26. {
  27. if (_titleBlock != null)
  28. {
  29. _titleBlock.MouseLeftButtonDown -= TitleBlock_OnMouseLeftButtonDown;
  30. }
  31. base.OnApplyTemplate();
  32. _mainBorder = GetTemplateChild(ElementMainBorder) as Border;
  33. _titleBlock = GetTemplateChild(ElementTitleBlock) as TextBlock;
  34. if (_titleBlock != null)
  35. {
  36. _titleBlock.MouseLeftButtonDown += TitleBlock_OnMouseLeftButtonDown;
  37. }
  38. if (PopupElement != null)
  39. {
  40. _mainBorder.Child = PopupElement;
  41. }
  42. }
  43. internal static readonly DependencyProperty ContentStrProperty = DependencyProperty.Register(
  44. nameof(ContentStr), typeof(string), typeof(PopupWindow), new PropertyMetadata(default(string)));
  45. internal string ContentStr
  46. {
  47. get => (string) GetValue(ContentStrProperty);
  48. set => SetValue(ContentStrProperty, value);
  49. }
  50. private bool IsDialog { get; set; }
  51. public PopupWindow()
  52. {
  53. CommandBindings.Add(new CommandBinding(ControlCommands.Close, CloseButton_OnClick));
  54. CommandBindings.Add(new CommandBinding(ControlCommands.Confirm, ButtonOk_OnClick));
  55. CommandBindings.Add(new CommandBinding(ControlCommands.Cancel, ButtonCancle_OnClick));
  56. Closing += (sender, args) =>
  57. {
  58. if (!IsDialog)
  59. Owner?.Focus();
  60. };
  61. Loaded += (s, e) =>
  62. {
  63. if (!_showBackground)
  64. {
  65. var point = ArithmeticHelper.CalSafePoint(_targetElement, PopupElement, BorderThickness);
  66. Left = point.X;
  67. Top = point.Y;
  68. Opacity = 1;
  69. }
  70. };
  71. try
  72. {
  73. Owner = Application.Current.MainWindow;
  74. }
  75. catch
  76. {
  77. // ignored
  78. }
  79. }
  80. public PopupWindow(System.Windows.Window owner) : this() => Owner = owner;
  81. private void CloseButton_OnClick(object sender, RoutedEventArgs e) => Close();
  82. public FrameworkElement PopupElement { get; set; }
  83. public static readonly DependencyProperty ShowTitleProperty = DependencyProperty.Register(
  84. nameof(ShowTitle), typeof(bool), typeof(PopupWindow), new PropertyMetadata(ValueBoxes.TrueBox));
  85. public bool ShowTitle
  86. {
  87. get => (bool) GetValue(ShowTitleProperty);
  88. set => SetValue(ShowTitleProperty, ValueBoxes.BooleanBox(value));
  89. }
  90. public static readonly DependencyProperty ShowCancelProperty = DependencyProperty.Register(
  91. nameof(ShowCancel), typeof(bool), typeof(PopupWindow), new PropertyMetadata(ValueBoxes.FalseBox));
  92. public bool ShowCancel
  93. {
  94. get => (bool) GetValue(ShowCancelProperty);
  95. set => SetValue(ShowCancelProperty, ValueBoxes.BooleanBox(value));
  96. }
  97. public static readonly DependencyProperty ShowBorderProperty = DependencyProperty.Register(
  98. nameof(ShowBorder), typeof(bool), typeof(PopupWindow), new PropertyMetadata(ValueBoxes.FalseBox));
  99. public bool ShowBorder
  100. {
  101. get => (bool) GetValue(ShowBorderProperty);
  102. set => SetValue(ShowBorderProperty, ValueBoxes.BooleanBox(value));
  103. }
  104. private void TitleBlock_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  105. {
  106. if (e.LeftButton == MouseButtonState.Pressed)
  107. {
  108. DragMove();
  109. }
  110. }
  111. public void Show(FrameworkElement element, bool showBackground = true)
  112. {
  113. if (!showBackground)
  114. {
  115. Opacity = 0;
  116. AllowsTransparency = true;
  117. WindowStyle = WindowStyle.None;
  118. ShowTitle = false;
  119. MinWidth = 0;
  120. MinHeight = 0;
  121. }
  122. _showBackground = showBackground;
  123. _targetElement = element;
  124. Show();
  125. }
  126. public void ShowDialog(FrameworkElement element, bool showBackground = true)
  127. {
  128. if (!showBackground)
  129. {
  130. Opacity = 0;
  131. AllowsTransparency = true;
  132. WindowStyle = WindowStyle.None;
  133. ShowTitle = false;
  134. MinWidth = 0;
  135. MinHeight = 0;
  136. }
  137. _showBackground = showBackground;
  138. _targetElement = element;
  139. ShowDialog();
  140. }
  141. public void Show(System.Windows.Window element, Point point)
  142. {
  143. Left = element.Left + point.X;
  144. Top = element.Top + point.Y;
  145. Show();
  146. }
  147. public static void Show(string message)
  148. {
  149. var window = new PopupWindow
  150. {
  151. AllowsTransparency = true,
  152. WindowStyle = WindowStyle.None,
  153. ContentStr = message,
  154. WindowStartupLocation = WindowStartupLocation.CenterScreen,
  155. Background = ResourceHelper.GetResourceInternal<Brush>(ResourceToken.PrimaryBrush)
  156. };
  157. window.Show();
  158. }
  159. public static bool? ShowDialog(string message, string title = default, bool showCancel = false)
  160. {
  161. var window = new PopupWindow
  162. {
  163. AllowsTransparency = true,
  164. WindowStyle = WindowStyle.None,
  165. ContentStr = message,
  166. IsDialog = true,
  167. WindowStartupLocation = WindowStartupLocation.CenterScreen,
  168. ShowBorder = true,
  169. Title = string.IsNullOrEmpty(title) ? Properties.Langs.Lang.Tip : title,
  170. ShowCancel = showCancel
  171. };
  172. return window.ShowDialog();
  173. }
  174. private void ButtonOk_OnClick(object sender, RoutedEventArgs e)
  175. {
  176. if (IsDialog)
  177. {
  178. DialogResult = true;
  179. }
  180. Close();
  181. }
  182. private void ButtonCancle_OnClick(object sender, RoutedEventArgs e)
  183. {
  184. if (IsDialog)
  185. {
  186. DialogResult = false;
  187. }
  188. Close();
  189. }
  190. protected override void OnClosed(EventArgs e)
  191. {
  192. base.OnClosed(e);
  193. PopupElement = null;
  194. }
  195. }