简化「WindowHeader」的视觉效果逻辑

This commit is contained in:
zengwenjie
2025-09-30 10:39:48 +08:00
parent 3c0e12ae9a
commit e8d6c06089
2 changed files with 27 additions and 21 deletions

View File

@@ -25,6 +25,11 @@
</ControlTemplate> </ControlTemplate>
</Setter.Value> </Setter.Value>
</Setter> </Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{Binding HoverBrush,RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
</Trigger>
</Style.Triggers>
</Style> </Style>
</ControlTemplate.Resources> </ControlTemplate.Resources>
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">

View File

@@ -8,7 +8,7 @@ using Shell = System.Windows.Shell;
namespace Deedy namespace Deedy
{ {
[ContentProperty("Child")] [ContentProperty("Header")]
[TemplatePart(Name = "MainMenu", Type = typeof(Menu))] [TemplatePart(Name = "MainMenu", Type = typeof(Menu))]
[TemplatePart(Name = "Minimize", Type = typeof(Button))] [TemplatePart(Name = "Minimize", Type = typeof(Button))]
[TemplatePart(Name = "Maximize", Type = typeof(Button))] [TemplatePart(Name = "Maximize", Type = typeof(Button))]
@@ -23,31 +23,35 @@ namespace Deedy
BackgroundProperty.OverrideMetadata(typeof(WindowHeader), new FrameworkPropertyMetadata(Brushes.DimGray)); BackgroundProperty.OverrideMetadata(typeof(WindowHeader), new FrameworkPropertyMetadata(Brushes.DimGray));
FontSizeProperty.OverrideMetadata(typeof(WindowHeader), new FrameworkPropertyMetadata(16.0)); FontSizeProperty.OverrideMetadata(typeof(WindowHeader), new FrameworkPropertyMetadata(16.0));
} }
/// <summary>
/// 悬停画刷
/// </summary>
public Brush HoverBrush public Brush HoverBrush
{ {
get { return (Brush)GetValue(HoverBrushProperty); } get { return (Brush)GetValue(HoverBrushProperty); }
set { SetValue(HoverBrushProperty, value); } set { SetValue(HoverBrushProperty, value); }
} }
public static readonly DependencyProperty HoverBrushProperty = public static readonly DependencyProperty HoverBrushProperty =
DependencyProperty.Register("HoverBrush", typeof(Brush), typeof(WindowHeader), new PropertyMetadata(Brushes.Silver)); DependencyProperty.Register("HoverBrush", typeof(Brush), typeof(WindowHeader), new PropertyMetadata(Brushes.Silver));
public UIElement Child /// <summary>
/// 标题
/// </summary>
public UIElement Header
{ {
get { return (UIElement)GetValue(ChildProperty); } get { return (UIElement)GetValue(HeaderProperty); }
set { SetValue(ChildProperty, value); } set { SetValue(HeaderProperty, value); }
} }
public static readonly DependencyProperty HeaderProperty =
public static readonly DependencyProperty ChildProperty = DependencyProperty.Register("Header", typeof(UIElement), typeof(WindowHeader), new PropertyMetadata(null,
DependencyProperty.Register("Child", typeof(UIElement), typeof(WindowHeader), new PropertyMetadata(null, (d, e) => (d, e) => (d as WindowHeader)?.Header_PropertyChangedCallback(e)));
/// <summary>
/// 处理「WindowHeader.Header」属性变更
/// </summary>
protected virtual void Header_PropertyChangedCallback(DependencyPropertyChangedEventArgs e)
{ {
WindowHeader windowHeader = (WindowHeader)d; if (e.NewValue is UIElement header && this.Container != null) this.Container.Child = header;
if (e.NewValue != null)
{
if (windowHeader.Container != null)
windowHeader.Container.Child = e.NewValue as UIElement;
} }
}));
/// <summary> /// <summary>
/// 菜单 /// 菜单
/// </summary> /// </summary>
@@ -123,7 +127,7 @@ namespace Deedy
this.CloseWin = GetTemplateChild("CloseWin") as Button; this.CloseWin = GetTemplateChild("CloseWin") as Button;
this.MainMenu = GetTemplateChild("MainMenu") as Menu; this.MainMenu = GetTemplateChild("MainMenu") as Menu;
if (this.Container != null) this.Container.Child = this.Child; if (this.Container != null) this.Container.Child = this.Header;
if (this.MainMenu != null) if (this.MainMenu != null)
{ {
this.MainMenu.Items.Clear(); this.MainMenu.Items.Clear();
@@ -149,10 +153,7 @@ namespace Deedy
{ {
foreach (var c in this.Controller.Children) foreach (var c in this.Controller.Children)
{ {
Button? button = c as Button; if(c is Button button)button.AddHandler(Button.ClickEvent,new RoutedEventHandler(this.CommandButton_Click));
button?.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.CommandButton_Click));
button?.AddHandler(Button.MouseEnterEvent, new RoutedEventHandler((s, e) => ((Button)e.Source).Background = this.HoverBrush));
button?.AddHandler(Button.MouseLeaveEvent, new RoutedEventHandler((s, e) => ((Button)e.Source).Background = Brushes.Transparent));
} }
} }
if (this.Menu != null) this.Menu.Height = this.ActualHeight; if (this.Menu != null) this.Menu.Height = this.ActualHeight;