WindowBase.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. namespace LRH.Controls
  9. {
  10. [TemplatePart(Name = PAET_MIN, Type = typeof(Button))]
  11. [TemplatePart(Name = PAET_RESTORE, Type = typeof(Button))]
  12. [TemplatePart(Name = PAET_MAX, Type = typeof(Button))]
  13. [TemplatePart(Name = PAET_CLOSE, Type = typeof(Button))]
  14. public class WindowBase:Window
  15. {
  16. static WindowBase()
  17. {
  18. DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowBase),
  19. new FrameworkPropertyMetadata(typeof(WindowBase)));
  20. MinButtonStyleKey = new ComponentResourceKey(typeof(WindowBase), "MinButtonStyle");
  21. RestoreButtonKey = new ComponentResourceKey(typeof(WindowBase), "RestoreButton");
  22. MaxButtonStyleKey = new ComponentResourceKey(typeof(WindowBase), "MaxButtonStyle");
  23. CloseButtonStyleKey = new ComponentResourceKey(typeof(WindowBase), "CloseButtonStyle");
  24. }
  25. #region 公开可改写键值信息
  26. public static ResourceKey MinButtonStyleKey { get; private set; }
  27. public static ResourceKey RestoreButtonKey { get; private set; }
  28. public static ResourceKey MaxButtonStyleKey { get; private set; }
  29. public static ResourceKey CloseButtonStyleKey { get; private set; }
  30. #endregion
  31. #region 关联子元素
  32. public const string PAET_MIN = "PAET_MIN";
  33. public const string PAET_RESTORE = "PAET_RESTORE";
  34. public const string PAET_MAX = "PAET_MAX";
  35. public const string PAET_CLOSE = "PAET_CLOSE";
  36. #endregion
  37. public override void OnApplyTemplate()
  38. {
  39. base.OnApplyTemplate();
  40. var minButton= GetTemplateChild(PAET_MIN) as Button;
  41. if(minButton!=null)
  42. {
  43. minButton.Click += (s, e) => this.WindowState = WindowState.Minimized;
  44. }
  45. var restoreButton = GetTemplateChild(PAET_RESTORE) as Button;
  46. if(restoreButton!=null)
  47. {
  48. restoreButton.Click += (s, e) => this.WindowState = WindowState.Normal;
  49. }
  50. var maxButton = GetTemplateChild(PAET_MAX) as Button;
  51. if(maxButton!=null)
  52. {
  53. maxButton.Click += (s, e) => this.WindowState = WindowState.Maximized;
  54. }
  55. var closeButton = GetTemplateChild(PAET_CLOSE) as Button;
  56. if(closeButton!=null)
  57. {
  58. closeButton.Click += (s, e) => this.Close();
  59. }
  60. }
  61. public static readonly DependencyProperty AttachMenuProperty = DependencyProperty.Register("AttachMenu",
  62. typeof(UIElement),
  63. typeof(WindowBase), new PropertyMetadata(null));
  64. public UIElement AttachMenu
  65. {
  66. get { return this.GetValue(AttachMenuProperty) as UIElement; }
  67. set { this.SetValue(AttachMenuProperty, value); }
  68. }
  69. }
  70. }