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.

MessageBoxCard.cs 9.7 KiB

1 vuosi sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using BPA.UIControl.Commons.KnownBoxes;
  2. using System;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Controls.Primitives;
  6. using System.Windows.Media;
  7. namespace BPA.UIControl
  8. {
  9. /// <summary>
  10. /// 消息框卡片
  11. /// </summary>
  12. [TemplatePart(Name = TransitionPartName, Type = typeof(Transition))]
  13. [TemplatePart(Name = OkButtonPartName, Type = typeof(Button))]
  14. [TemplatePart(Name = CancelButtonPartName, Type = typeof(Button))]
  15. [TemplatePart(Name = YesButtonPartName, Type = typeof(Button))]
  16. [TemplatePart(Name = NoButtonPartName, Type = typeof(Button))]
  17. public class MessageBoxCard : Control
  18. {
  19. /// <summary>
  20. /// 转换动画名称
  21. /// </summary>
  22. public const string TransitionPartName = "Path_Transition";
  23. /// <summary>
  24. /// 确认按钮名称
  25. /// </summary>
  26. public const string OkButtonPartName = "PART_OkButton";
  27. /// <summary>
  28. /// 取消按钮名称
  29. /// </summary>
  30. public const string CancelButtonPartName = "PART_CancelButton";
  31. /// <summary>
  32. /// 是按钮名称
  33. /// </summary>
  34. public const string YesButtonPartName = "PART_YesButton";
  35. /// <summary>
  36. /// 否按钮名称
  37. /// </summary>
  38. public const string NoButtonPartName = "PART_NoButton";
  39. /// <summary>
  40. /// 消息框结果事件处理
  41. /// </summary>
  42. /// <param name="sender"></param>
  43. /// <param name="e"></param>
  44. public delegate void MessageBoxResultRoutedEventHandler(object sender, MessageBoxResultRoutedEventArgs e);
  45. static MessageBoxCard()
  46. {
  47. DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageBoxCard), new FrameworkPropertyMetadata(typeof(MessageBoxCard)));
  48. }
  49. /// <inheritdoc/>
  50. public override void OnApplyTemplate()
  51. {
  52. base.OnApplyTemplate();
  53. if (GetTemplateChild(OkButtonPartName) is ButtonBase okButton)
  54. {
  55. okButton.Click += (sender, args) => InternalReturnResult(this, MessageBoxResult.OK);
  56. }
  57. if (GetTemplateChild(CancelButtonPartName) is ButtonBase cancelButton)
  58. {
  59. cancelButton.Click += (sender, args) => InternalReturnResult(this, MessageBoxResult.Cancel);
  60. }
  61. if (GetTemplateChild(YesButtonPartName) is ButtonBase yesButton)
  62. {
  63. yesButton.Click += (sender, args) => InternalReturnResult(this, MessageBoxResult.Yes);
  64. }
  65. if (GetTemplateChild(NoButtonPartName) is ButtonBase noButton)
  66. {
  67. noButton.Click += (sender, args) => InternalReturnResult(this, MessageBoxResult.No);
  68. }
  69. if (GetTemplateChild(TransitionPartName) is Transition transition)
  70. {
  71. transition.Closed += (sender, e) =>
  72. {
  73. RoutedEventArgs eventArgs = new RoutedEventArgs(ClosedEvent, this);
  74. this.RaiseEvent(eventArgs);
  75. };
  76. }
  77. }
  78. #region 事件
  79. /// <summary>
  80. /// 返回结果事件
  81. /// </summary>
  82. public static readonly RoutedEvent ReturnResultEvent = EventManager.RegisterRoutedEvent("ReturnResult", RoutingStrategy.Bubble, typeof(MessageBoxResultRoutedEventHandler), typeof(MessageBoxCard));
  83. /// <summary>
  84. /// /// <summary>
  85. /// 返回结果事件处理
  86. /// </summary>
  87. /// </summary>
  88. public event MessageBoxResultRoutedEventHandler ReturnResult
  89. {
  90. add { AddHandler(ReturnResultEvent, value); }
  91. remove { RemoveHandler(ReturnResultEvent, value); }
  92. }
  93. /// <summary>
  94. /// 关闭消息事件
  95. /// </summary>
  96. public static readonly RoutedEvent ClosedEvent = EventManager.RegisterRoutedEvent("Close", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MessageBoxCard));
  97. /// <summary>
  98. /// 关闭消息事件处理
  99. /// </summary>
  100. public event RoutedEventHandler Closed
  101. {
  102. add { AddHandler(ClosedEvent, value); }
  103. remove { RemoveHandler(ClosedEvent, value); }
  104. }
  105. #endregion 事件
  106. #region 依赖属性
  107. /// <summary>
  108. /// 是否显示
  109. /// </summary>
  110. public static readonly DependencyProperty IsShowProperty =
  111. DependencyProperty.Register("IsShow", typeof(bool), typeof(MessageBoxCard), new PropertyMetadata(BooleanBoxes.FalseBox));
  112. /// <summary>
  113. /// 是否显示
  114. /// </summary>
  115. public bool IsShow
  116. {
  117. get { return (bool)GetValue(IsShowProperty); }
  118. set { SetValue(IsShowProperty, BooleanBoxes.Box(value)); }
  119. }
  120. /// <summary>
  121. /// 消息内容
  122. /// </summary>
  123. public static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
  124. "Message", typeof(string), typeof(MessageBoxCard), new PropertyMetadata(default(string)));
  125. /// <summary>
  126. /// 消息内容
  127. /// </summary>
  128. public string Message
  129. {
  130. get { return (string)GetValue(MessageProperty); }
  131. set { SetValue(MessageProperty, value); }
  132. }
  133. /// <summary>
  134. /// 标题
  135. /// </summary>
  136. public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
  137. "Title", typeof(string), typeof(MessageBoxCard), new PropertyMetadata(default(string)));
  138. /// <summary>
  139. /// 标题
  140. /// </summary>
  141. public string Title
  142. {
  143. get { return (string)GetValue(TitleProperty); }
  144. set { SetValue(TitleProperty, value); }
  145. }
  146. /// <summary>
  147. /// 圆角半径
  148. /// </summary>
  149. public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register(
  150. "CornerRadius", typeof(CornerRadius), typeof(MessageBoxCard), new PropertyMetadata(default(CornerRadius)));
  151. /// <summary>
  152. /// 圆角半径
  153. /// </summary>
  154. public CornerRadius CornerRadius
  155. {
  156. get { return (CornerRadius)GetValue(CornerRadiusProperty); }
  157. set { SetValue(CornerRadiusProperty, value); }
  158. }
  159. /// <summary>
  160. /// 主题颜色
  161. /// </summary>
  162. public static readonly DependencyProperty ThemeColorBrushProperty = DependencyProperty.Register(
  163. "ThemeColorBrush", typeof(Brush), typeof(MessageBoxCard), new PropertyMetadata(default(Brush)));
  164. /// <summary>
  165. /// 主题颜色
  166. /// </summary>
  167. public Brush ThemeColorBrush
  168. {
  169. get { return (Brush)GetValue(ThemeColorBrushProperty); }
  170. set { SetValue(ThemeColorBrushProperty, value); }
  171. }
  172. /// <summary>
  173. /// 图标类型
  174. /// </summary>
  175. public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(
  176. "Type", typeof(MessageBoxType), typeof(MessageBoxCard), new PropertyMetadata(default(MessageBoxType)));
  177. /// <summary>
  178. /// 图标类型
  179. /// </summary>
  180. public MessageBoxType Type
  181. {
  182. get { return (MessageBoxType)GetValue(TypeProperty); }
  183. set { SetValue(TypeProperty, value); }
  184. }
  185. /// <summary>
  186. /// 是否显示阴影
  187. /// </summary>
  188. public static readonly DependencyProperty ShowShadowProperty = DependencyProperty.Register(
  189. "ShowShadow", typeof(bool), typeof(MessageBoxCard), new PropertyMetadata(BooleanBoxes.FalseBox));
  190. /// <summary>
  191. /// 是否显示阴影
  192. /// </summary>
  193. public bool ShowShadow
  194. {
  195. get { return (bool)GetValue(ShowShadowProperty); }
  196. set { SetValue(ShowShadowProperty, BooleanBoxes.Box(value)); }
  197. }
  198. /// <summary>
  199. /// 消息框按钮类型
  200. /// </summary>
  201. public static readonly DependencyProperty MessageBoxButtonProperty = DependencyProperty.Register(
  202. "MessageBoxButton", typeof(MessageBoxButton), typeof(MessageBoxCard), new PropertyMetadata(default(MessageBoxButton)));
  203. /// <summary>
  204. /// 消息框按钮类型
  205. /// </summary>
  206. public MessageBoxButton MessageBoxButton
  207. {
  208. get { return (MessageBoxButton)GetValue(MessageBoxButtonProperty); }
  209. set { SetValue(MessageBoxButtonProperty, value); }
  210. }
  211. #endregion 依赖属性
  212. private void InternalReturnResult(MessageBoxCard card, MessageBoxResult result)
  213. {
  214. IsShow = false;
  215. MessageBoxResultRoutedEventArgs eventArgs = new MessageBoxResultRoutedEventArgs(ReturnResultEvent, result, card);
  216. card.RaiseEvent(eventArgs);
  217. }
  218. }
  219. /// <summary>
  220. /// 消息框框结果路由参数
  221. /// </summary>
  222. public class MessageBoxResultRoutedEventArgs : RoutedEventArgs
  223. {
  224. /// <summary>
  225. /// 结果
  226. /// </summary>
  227. public MessageBoxResult Result { get; set; }
  228. /// <summary>
  229. /// 消息框
  230. /// </summary>
  231. public MessageBoxCard Card { get; set; }
  232. /// <summary>
  233. /// Initializes a new instance of the <see cref="MessageBoxResultRoutedEventArgs"/> class.
  234. /// </summary>
  235. /// <param name="routedEvent">The routed event.</param>
  236. /// <param name="result">The result.</param>
  237. /// <param name="card">消息框卡片</param>
  238. public MessageBoxResultRoutedEventArgs(RoutedEvent routedEvent, MessageBoxResult result, MessageBoxCard card) : base(routedEvent)
  239. {
  240. Result = result;
  241. Card = card;
  242. }
  243. }
  244. }