using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
namespace Muchinfo.WPF.Controls.Windows
{
///
/// Class WindowBase.
///
[TemplatePart(Name = "btnClose", Type = typeof(System.Windows.Controls.Button))]
[TemplatePart(Name = "btnMin", Type = typeof(System.Windows.Controls.Button))]
[TemplatePart(Name = "brdTitle", Type = typeof(System.Windows.Controls.Border))]
[TemplatePart(Name = "btnMax", Type = typeof(System.Windows.Controls.Button))]
//[TemplatePart(Name = "WindowBorder", Type = typeof(Border))]
public class WindowBase : Window
{
static WindowBase()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowBase), new FrameworkPropertyMetadata(typeof(WindowBase)));
}
private System.Windows.Controls.Button _btnClose;
private System.Windows.Controls.Border _brdTitle;
private System.Windows.Controls.Button _btnMin;
private System.Windows.Controls.Button _btnMax;
//private Border winborborder;
public WindowBase()
{
WindowStartupLocation = WindowStartupLocation.CenterOwner;
this.Loaded += delegate
{
InitializeEvent();
ShowInTaskbar = this.Owner == null;
};
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_btnClose = GetTemplateChild("btnClose") as System.Windows.Controls.Button;
_brdTitle = GetTemplateChild("brdTitle") as System.Windows.Controls.Border;
_btnMin = GetTemplateChild("btnMin") as System.Windows.Controls.Button;
_btnMax = GetTemplateChild("btnMax") as System.Windows.Controls.Button;
//winborborder = GetTemplateChild("WindowBorder") as Border;
}
private void InitializeEvent()
{
if (_btnMin != null)
{
_btnMin.Click += delegate
{
};
}
if (_btnClose != null)
{
_btnClose.Click += delegate
{
this.Close();
};
}
if (_btnMax != null)
{
_btnMax.Click += delegate
{
this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
};
}
if (_brdTitle != null)
{
_brdTitle.MouseMove += delegate(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
this.DragMove();
}
};
}
//只手动抬头移动窗口
//if (winborborder != null)
//{
// winborborder.MouseMove += delegate(object sender, MouseEventArgs e)
// {
// if (e.LeftButton == MouseButtonState.Pressed)
// {
// this.DragMove();
// }
// };
//}
}
///
/// 窗口圆角
///
public CornerRadius CornerRadius
{
get { return (CornerRadius)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(WindowBase), new PropertyMetadata(new CornerRadius(2)));
public Brush TitleBackGround
{
get { return (Brush)GetValue(TitleBackGroundProperty); }
set { SetValue(TitleBackGroundProperty, value); }
}
// Using a DependencyProperty as the backing store for TitleBackGround. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleBackGroundProperty =
DependencyProperty.Register("TitleBackGround", typeof(Brush), typeof(WindowBase), new PropertyMetadata(Brushes.Bisque));
public Brush TitleForeground
{
get { return (Brush)GetValue(TitleForegroundProperty); }
set { SetValue(TitleForegroundProperty, value); }
}
// Using a DependencyProperty as the backing store for TitleFordBrush. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleForegroundProperty =
DependencyProperty.Register("TitleForeground", typeof(Brush), typeof(WindowBase), new PropertyMetadata(Brushes.Black));
///
/// 关闭按钮样式
///
public Style CloseButtonStyle
{
get { return (Style)GetValue(CloseButtonStyleProperty); }
set { SetValue(CloseButtonStyleProperty, value); }
}
// Using a DependencyProperty as the backing store for CloseButtonStyle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CloseButtonStyleProperty =
DependencyProperty.Register("CloseButtonStyle", typeof(Style), typeof(WindowBase), new PropertyMetadata(null));
///
/// 最大最小化
///
public Style MaxBtnStyle
{
get { return (Style)GetValue(MaxBtnStyleProperty); }
set { SetValue(MaxBtnStyleProperty, value); }
}
// Using a DependencyProperty as the backing store for MaxStyle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MaxBtnStyleProperty =
DependencyProperty.Register("MaxBtnStyle", typeof(Style), typeof(WindowBase), new PropertyMetadata(null));
///
/// 是否可最大化最小化
///
public Visibility MaxBtnVisibility
{
get { return (Visibility)GetValue(MaxBtnVisibilityProperty); }
set { SetValue(MaxBtnVisibilityProperty, value); }
}
// Using a DependencyProperty as the backing store for MaxBtnVisibility. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MaxBtnVisibilityProperty =
DependencyProperty.Register("MaxBtnVisibility", typeof(Visibility), typeof(WindowBase), new PropertyMetadata(Visibility.Collapsed));
public ICommand ClosedCommand
{
get { return (ICommand)GetValue(ClosedCommandProperty); }
set { SetValue(ClosedCommandProperty, value); }
}
// Using a DependencyProperty as the backing store for ClosedCommand. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ClosedCommandProperty =
DependencyProperty.Register("ClosedCommand", typeof(ICommand), typeof(WindowBase), new PropertyMetadata(null));
protected override void OnClosed(EventArgs e)
{
if (ClosedCommand != null)
{
ClosedCommand.Execute(this);
}
base.OnClosed(e);
}
}
}