From 314e3e3ebd0222baf40304565872a8b93c82a9b0 Mon Sep 17 00:00:00 2001 From: zengwenjie <1663900244@qq.com> Date: Tue, 30 Sep 2025 12:57:51 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=8CWindowHeader=E3=80=8D=E4=B8=8E?= =?UTF-8?q?=E3=80=8CWindowBorder=E3=80=8D=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DeedyDesigner/Deedy.Testing/MainWindow.xaml | 1 + .../Deedy.Testing/MainWindow.xaml.cs | 5 ++ .../Deedy.Testing/WindowBorderTest.xaml | 25 +++++++++ .../Deedy.Testing/WindowBorderTest.xaml.cs | 37 +++++++++++++ .../Deedy.Testing/WindowHeaderTest.xaml | 2 +- DeedyDesigner/Deedy.Wpf/Themes/Generic.xaml | 53 ++++++++++++++++--- DeedyDesigner/Deedy.Wpf/WindowBorder.cs | 41 +++++++++----- DeedyDesigner/Deedy.Wpf/WindowHeader.cs | 14 +++-- 8 files changed, 153 insertions(+), 25 deletions(-) create mode 100644 DeedyDesigner/Deedy.Testing/WindowBorderTest.xaml create mode 100644 DeedyDesigner/Deedy.Testing/WindowBorderTest.xaml.cs diff --git a/DeedyDesigner/Deedy.Testing/MainWindow.xaml b/DeedyDesigner/Deedy.Testing/MainWindow.xaml index 17cb653..ece0eb1 100644 --- a/DeedyDesigner/Deedy.Testing/MainWindow.xaml +++ b/DeedyDesigner/Deedy.Testing/MainWindow.xaml @@ -19,6 +19,7 @@ + + + + + + + + + diff --git a/DeedyDesigner/Deedy.Wpf/WindowBorder.cs b/DeedyDesigner/Deedy.Wpf/WindowBorder.cs index 95511d5..d875bfb 100644 --- a/DeedyDesigner/Deedy.Wpf/WindowBorder.cs +++ b/DeedyDesigner/Deedy.Wpf/WindowBorder.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls; using System.Windows.Data; @@ -19,12 +20,11 @@ namespace Deedy [TemplatePart(Name = "TitleBar", Type = typeof(Control))] [TemplatePart(Name = "Controller", Type = typeof(Panel))] [TemplatePart(Name = "Container", Type = typeof(Decorator))] - public class WindowBorder : HeaderedContentControl + public class WindowBorder : ContentControl { static WindowBorder() { DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowBorder), new FrameworkPropertyMetadata(typeof(WindowBorder))); - BackgroundProperty.OverrideMetadata(typeof(WindowBorder), new FrameworkPropertyMetadata(Brushes.Gray)); } /// /// 悬停画刷 @@ -55,14 +55,24 @@ namespace Deedy set { SetValue(HeaderHeightProperty, value); } } public static readonly DependencyProperty HeaderHeightProperty = - DependencyProperty.Register("HeaderHeight", typeof(double), typeof(WindowBorder), new PropertyMetadata(40.0, - (d, e) => (d as WindowBorder)?.HeaderHeight_PropertyChangedCallback(e))); + DependencyProperty.Register("HeaderHeight", typeof(double), typeof(WindowBorder), new PropertyMetadata(40.0)); /// - /// 处理「WindowBorder.HeaderHeight」属性变更 + /// 标题 /// - protected virtual void HeaderHeight_PropertyChangedCallback(DependencyPropertyChangedEventArgs e) + public UIElement Header { - if ((double)e.NewValue < 32) this.HeaderHeight = 32; + get { return (UIElement)GetValue(HeaderProperty); } + set { SetValue(HeaderProperty, value); } + } + public static readonly DependencyProperty HeaderProperty = + DependencyProperty.Register("Header", typeof(UIElement), typeof(WindowBorder), new PropertyMetadata(null, + (d, e) => (d as WindowBorder)?.Header_PropertyChangedCallback(e))); + /// + /// 处理「WindowBorder.Header」属性变更 + /// + protected virtual void Header_PropertyChangedCallback(DependencyPropertyChangedEventArgs e) + { + if (e.NewValue is UIElement header && this.Container != null) this.Container.Child = header; } /// /// 菜单 @@ -141,12 +151,13 @@ namespace Deedy this.TitleBar = GetTemplateChild("TitleBar") as Control; this.MainMenu = GetTemplateChild("MainMenu") as Menu; + if (this.Container != null) this.Container.Child = this.Header; if (this.MainMenu != null) { this.MainMenu.Items.Clear(); if (this.Menu != null) { - this.Menu.Height = this.Height; + this.Menu.Height = this.HeaderHeight; this.Menu.Background = Brushes.Transparent; this.MainMenu.Items.Add(this.Menu); } @@ -215,11 +226,15 @@ namespace Deedy { if (this.Target != null) { - var winContent = this.Target.Content as FrameworkElement; - if (winContent == null) return; + if (this.Target.Content is not FrameworkElement winContent) return; if (this.Target.WindowState == WindowState.Maximized) + { winContent.Margin = new Thickness(8); - else winContent.Margin = new Thickness(0); + } + else + { + winContent.Margin = new Thickness(1, 0, 1, 1); + } } } private void Target_StateChanged(object? sender, EventArgs e) @@ -228,9 +243,9 @@ namespace Deedy } private void CommandButton_Click(object sender, RoutedEventArgs e) { - Button? button = sender as Button; - if (button != null) + if (sender is Button button) { + e.Handled = true; switch (button.Name) { case "Minimize": diff --git a/DeedyDesigner/Deedy.Wpf/WindowHeader.cs b/DeedyDesigner/Deedy.Wpf/WindowHeader.cs index 05ff5b3..bc68eb1 100644 --- a/DeedyDesigner/Deedy.Wpf/WindowHeader.cs +++ b/DeedyDesigner/Deedy.Wpf/WindowHeader.cs @@ -204,11 +204,15 @@ namespace Deedy { if (this.Target != null) { - var winContent = this.Target.Content as FrameworkElement; - if (winContent == null) return; + if (this.Target.Content is not FrameworkElement winContent) return; if (this.Target.WindowState == WindowState.Maximized) + { winContent.Margin = new Thickness(8); - else winContent.Margin = new Thickness(0); + } + else + { + winContent.Margin = new Thickness(1, 0, 1, 1); + } } } private void Target_StateChanged(object? sender, EventArgs e) @@ -217,9 +221,9 @@ namespace Deedy } private void CommandButton_Click(object sender, RoutedEventArgs e) { - Button? button = sender as Button; - if (button != null) + if (sender is Button button) { + e.Handled = true; switch (button.Name) { case "Minimize":