using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace LRH.Controls { [TemplatePart(Name = PAET_MIN, Type = typeof(Button))] [TemplatePart(Name = PAET_RESTORE, Type = typeof(Button))] [TemplatePart(Name = PAET_MAX, Type = typeof(Button))] [TemplatePart(Name = PAET_CLOSE, Type = typeof(Button))] public class WindowBase:Window { static WindowBase() { DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowBase), new FrameworkPropertyMetadata(typeof(WindowBase))); MinButtonStyleKey = new ComponentResourceKey(typeof(WindowBase), "MinButtonStyle"); RestoreButtonKey = new ComponentResourceKey(typeof(WindowBase), "RestoreButton"); MaxButtonStyleKey = new ComponentResourceKey(typeof(WindowBase), "MaxButtonStyle"); CloseButtonStyleKey = new ComponentResourceKey(typeof(WindowBase), "CloseButtonStyle"); } #region 公开可改写键值信息 public static ResourceKey MinButtonStyleKey { get; private set; } public static ResourceKey RestoreButtonKey { get; private set; } public static ResourceKey MaxButtonStyleKey { get; private set; } public static ResourceKey CloseButtonStyleKey { get; private set; } #endregion #region 关联子元素 public const string PAET_MIN = "PAET_MIN"; public const string PAET_RESTORE = "PAET_RESTORE"; public const string PAET_MAX = "PAET_MAX"; public const string PAET_CLOSE = "PAET_CLOSE"; #endregion public override void OnApplyTemplate() { base.OnApplyTemplate(); var minButton= GetTemplateChild(PAET_MIN) as Button; if(minButton!=null) { minButton.Click += (s, e) => this.WindowState = WindowState.Minimized; } var restoreButton = GetTemplateChild(PAET_RESTORE) as Button; if(restoreButton!=null) { restoreButton.Click += (s, e) => this.WindowState = WindowState.Normal; } var maxButton = GetTemplateChild(PAET_MAX) as Button; if(maxButton!=null) { maxButton.Click += (s, e) => this.WindowState = WindowState.Maximized; } var closeButton = GetTemplateChild(PAET_CLOSE) as Button; if(closeButton!=null) { closeButton.Click += (s, e) => this.Close(); } } public static readonly DependencyProperty AttachMenuProperty = DependencyProperty.Register("AttachMenu", typeof(UIElement), typeof(WindowBase), new PropertyMetadata(null)); public UIElement AttachMenu { get { return this.GetValue(AttachMenuProperty) as UIElement; } set { this.SetValue(AttachMenuProperty, value); } } } }