Files
ShrlAlgoToolkit/NeuWPF/NeoUI/Controls/WindowStyle.xaml.cs

137 lines
4.7 KiB
C#
Raw Normal View History

2025-08-20 12:10:35 +08:00
namespace NeoUI.Controls;
2025-08-12 23:08:54 +08:00
2025-08-20 12:10:35 +08:00
/// <summary>
/// NeoWindow 类继承自 WPF 的 Window 类,用于创建具有特定样式和功能的窗口。
/// 该类通过设置默认样式资源引用来自定义窗口外观,并提供属性来控制窗口的行为和内容布局。
/// </summary>
public class NeoWindow : Window
2025-08-12 23:08:54 +08:00
{
private const string VbMinimizeButtonName = "minimizeButton";
private const string VbMaximizeRestoreButtonName = "maximizeRestoreButton";
2025-08-20 12:10:35 +08:00
/// <summary>
/// 用于在XAML模板中标识关闭按钮的名称。
/// 此常量字符串值为 "closeButton"用于在窗口样式应用时通过GetTemplateChild方法查找对应的关闭按钮控件。
/// </summary>
2025-08-12 23:08:54 +08:00
private const string VbCloseButtonName = "closeButton";
2025-08-20 12:10:35 +08:00
/// <summary>
///
/// </summary>
public NeoWindow()
2025-08-12 23:08:54 +08:00
{
2025-08-20 12:10:35 +08:00
SetResourceReference(StyleProperty, typeof(NeoWindow)); //设置默认的样式资源引用
2025-08-12 23:08:54 +08:00
}
2025-08-20 12:10:35 +08:00
static NeoWindow()
2025-08-12 23:08:54 +08:00
{
2025-08-20 12:10:35 +08:00
DefaultStyleKeyProperty.OverrideMetadata(typeof(NeoWindow), new FrameworkPropertyMetadata(typeof(NeoWindow)));
2025-08-12 23:08:54 +08:00
}
/// <summary>
/// 如果窗口不活动,请让窗口的内容淡出。
/// 默认值为真(已启用)。
/// </summary>
public static readonly DependencyProperty FadeContentIfInactiveProperty = DependencyProperty.Register(
2025-08-20 12:10:35 +08:00
nameof(FadeContentIfInactive), typeof(bool), typeof(NeoWindow), new FrameworkPropertyMetadata(true));
2025-08-12 23:08:54 +08:00
/// <summary>
/// 如果窗口不活动,请让窗口的内容淡出。
/// 默认值为真(已启用)。
/// </summary>
public bool FadeContentIfInactive
{
get => (bool)GetValue(FadeContentIfInactiveProperty);
set => SetValue(FadeContentIfInactiveProperty, value);
}
/// <summary>
/// 标题栏左侧内容模板
/// </summary>
2025-08-20 12:10:35 +08:00
public object TitleBarLeftContent
2025-08-12 23:08:54 +08:00
{
2025-08-20 12:10:35 +08:00
get => (object)GetValue(TitleBarLeftContentProperty);
set => SetValue(TitleBarLeftContentProperty, value);
2025-08-12 23:08:54 +08:00
}
2025-08-20 12:10:35 +08:00
/// <summary>
/// 用于定义窗口左侧内容区域的数据模板。
/// 此依赖属性允许开发者自定义左侧内容的布局和显示方式通过设置不同的DataTemplate来改变该区域的内容呈现形式。
/// </summary>
public static readonly DependencyProperty TitleBarLeftContentProperty =
DependencyProperty.Register(nameof(TitleBarLeftContent), typeof(object), typeof(NeoWindow));
2025-08-12 23:08:54 +08:00
/// <summary>
/// 标题栏右侧内容模板
/// </summary>
2025-08-20 12:10:35 +08:00
public object TitleBarRightContent
2025-08-12 23:08:54 +08:00
{
2025-08-20 12:10:35 +08:00
get => (object)GetValue(TitleBarRightContentProperty);
set => SetValue(TitleBarRightContentProperty, value);
2025-08-12 23:08:54 +08:00
}
2025-08-20 12:10:35 +08:00
/// <summary>
/// 用于定义窗口右侧内容区域的数据模板。
/// 此依赖属性允许开发者自定义右侧内容的布局和显示方式通过设置不同的DataTemplate来改变该区域的内容呈现形式。
/// </summary>
public static readonly DependencyProperty TitleBarRightContentProperty =
DependencyProperty.Register(nameof(TitleBarRightContent), typeof(object), typeof(NeoWindow));
private Button? minimizeButton;
private Button? maximizeRestoreButton;
private Button? closeButton;
/// <inheritdoc />
2025-08-12 23:08:54 +08:00
public override void OnApplyTemplate()
{
if (minimizeButton != null)
{
minimizeButton.Click -= MinimizeButtonClickHandler;
}
minimizeButton = GetTemplateChild(VbMinimizeButtonName) as Button;
if (minimizeButton != null)
{
minimizeButton.Click += MinimizeButtonClickHandler;
}
if (maximizeRestoreButton != null)
{
maximizeRestoreButton.Click -= MaximizeRestoreButtonClickHandler;
}
maximizeRestoreButton = GetTemplateChild(VbMaximizeRestoreButtonName) as Button;
if (maximizeRestoreButton != null)
{
maximizeRestoreButton.Click += MaximizeRestoreButtonClickHandler;
}
if (closeButton != null)
{
closeButton.Click -= CloseButtonClickHandler;
}
closeButton = GetTemplateChild(VbCloseButtonName) as Button;
if (closeButton != null)
{
closeButton.Click += CloseButtonClickHandler;
}
base.OnApplyTemplate();
}
private void CloseButtonClickHandler(object sender, RoutedEventArgs args)
{
Close();
}
private void MaximizeRestoreButtonClickHandler(object sender, RoutedEventArgs args)
{
WindowState = (WindowState == WindowState.Normal) ? WindowState.Maximized : WindowState.Normal;
}
private void MinimizeButtonClickHandler(object sender, RoutedEventArgs args)
{
WindowState = WindowState.Minimized;
}
}