简化「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>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{Binding HoverBrush,RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
</Trigger>
</Style.Triggers>
</Style>
</ControlTemplate.Resources>
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">

View File

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