编写窗口模版,未成功实现

This commit is contained in:
于智纯
2025-09-21 17:14:26 +08:00
parent b66885c2b6
commit 2db765f42d
3 changed files with 131 additions and 33 deletions

View File

@@ -16,33 +16,7 @@ using System.Windows.Shapes;
namespace Deedy namespace Deedy
{ {
/// <summary> /// <summary>
/// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。 /// 带自定义标题头的简约窗口
///
/// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
/// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
/// 元素中:
///
/// xmlns:MyNamespace="clr-namespace:Deedy"
///
///
/// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
/// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
/// 元素中:
///
/// xmlns:MyNamespace="clr-namespace:Deedy;assembly=Deedy"
///
/// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
/// 并重新生成以避免编译错误:
///
/// 在解决方案资源管理器中右击目标项目,然后依次单击
/// “添加引用”->“项目”->[浏览查找并选择此项目]
///
///
/// 步骤 2)
/// 继续操作并在 XAML 文件中使用控件。
///
/// <MyNamespace:ElegantWindow/>
///
/// </summary> /// </summary>
[TemplatePart(Name = "PART_MinButton", Type = typeof(Button))] [TemplatePart(Name = "PART_MinButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_MaxButton", Type = typeof(Button))] [TemplatePart(Name = "PART_MaxButton", Type = typeof(Button))]
@@ -60,6 +34,67 @@ namespace Deedy
public override void OnApplyTemplate() public override void OnApplyTemplate()
{ {
base.OnApplyTemplate(); base.OnApplyTemplate();
} var close = this.GetTemplateChild("PART_CloseButton") as Button;
}
/// <summary>
/// 窗口标题高度
/// </summary>
public double HeaderHeight
{
get { return (double)GetValue(HeaderHeightProperty); }
set { SetValue(HeaderHeightProperty, value); }
}
public static readonly DependencyProperty HeaderHeightProperty =
DependencyProperty.Register("HeaderHeight", typeof(double), typeof(ElegantWindow), new PropertyMetadata(32.0));
/// <summary>
/// 背景画刷「鼠标悬停」
/// </summary>
public Brush HoverBackground
{
get { return (Brush)GetValue(HoverBackgroundProperty); }
set { SetValue(HoverBackgroundProperty, value); }
}
public static readonly DependencyProperty HoverBackgroundProperty =
DependencyProperty.Register("HoverBackground", typeof(Brush), typeof(ElegantWindow), new PropertyMetadata(Brushes.LightGray));
/// <summary>
/// 前景画刷「鼠标悬停」
/// </summary>
public Brush HoverForeground
{
get { return (Brush)GetValue(HoverForegroundProperty); }
set { SetValue(HoverForegroundProperty, value); }
}
public static readonly DependencyProperty HoverForegroundProperty =
DependencyProperty.Register("HoverForeground", typeof(Brush), typeof(ElegantWindow), new PropertyMetadata(Brushes.LightPink));
/// <summary>
/// 窗口标题
/// </summary>
public object Header
{
get { return (object)GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("Header", typeof(object), typeof(ElegantWindow), new PropertyMetadata(null));
/// <summary>
/// 标题数据模版
/// </summary>
public DataTemplate HeaderTemplate
{
get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
set { SetValue(HeaderTemplateProperty, value); }
}
public static readonly DependencyProperty HeaderTemplateProperty =
DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(ElegantWindow), new PropertyMetadata(null));
/// <summary>
/// 窗口标题背景
/// </summary>
public Brush TitleBackground
{
get { return (Brush)GetValue(TitleBackgroundProperty); }
set { SetValue(TitleBackgroundProperty, value); }
}
public static readonly DependencyProperty TitleBackgroundProperty =
DependencyProperty.Register("TitleBackground", typeof(Brush), typeof(ElegantWindow), new PropertyMetadata(Brushes.Silver));
} }
} }

View File

@@ -28,7 +28,8 @@ namespace Deedy
private void Button_Click(object sender, RoutedEventArgs e) private void Button_Click(object sender, RoutedEventArgs e)
{ {
DrawerManager.ShowDrawer(this.decorator, new Border() { Background = Brushes.Red, MinWidth = 400, MinHeight = 400, HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch }); //DrawerManager.ShowDrawer(this.decorator, new Border() { Background = Brushes.Red, MinWidth = 400, MinHeight = 400, HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch });
new ElegantWindow().ShowDialog();
} }
} }
} }

View File

@@ -69,12 +69,74 @@
</Style> </Style>
<Style TargetType="{x:Type local:ElegantWindow}"> <Style TargetType="{x:Type local:ElegantWindow}">
<Setter Property="Background" Value="Gray"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="BorderBrush" Value="DarkGray"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Template"> <Setter Property="Template">
<Setter.Value> <Setter.Value>
<ControlTemplate TargetType="{x:Type local:ElegantWindow}"> <ControlTemplate TargetType="{x:Type local:ElegantWindow}">
<Border Background="{TemplateBinding Background}" <ControlTemplate.Resources>
BorderBrush="{TemplateBinding BorderBrush}" <Style TargetType="{x:Type Button}">
BorderThickness="{TemplateBinding BorderThickness}"> <Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Border x:Name="normal" Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ElegantWindow},Path=Background}">
<Image>
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<GeometryDrawing Brush="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:ElegantWindow},Path=Foreground}"
Geometry="{TemplateBinding Content}"/>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
</Border>
<Border x:Name="hover" Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ElegantWindow},Path=HoverBackground}">
<Image>
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<GeometryDrawing Brush="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:ElegantWindow},Path=HoverForeground}"
Geometry="{TemplateBinding Content}"/>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="normal" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="hover" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="normal" Property="Visibility" Value="Visible"/>
<Setter TargetName="hover" Property="Visibility" Value="Collapsed"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ControlTemplate.Resources>
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<DockPanel>
<DockPanel Height="{TemplateBinding HeaderHeight}" DockPanel.Dock="Top">
<Border Width="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}">
<Image Source="{TemplateBinding Icon}" HorizontalAlignment="Center" VerticalAlignment="Center"
MaxHeight="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}},Path=ActualHeight}"
MaxWidth="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}"/>
</Border>
<Button x:Name="PART_CloseButton" Width="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualHeight}">
<PathGeometry>m12 10.93 5.719-5.72c.146-.146.339-.219.531-.219.404 0 .75.324.75.749 0 .193-.073.385-.219.532l-5.72 5.719 5.719 5.719c.147.147.22.339.22.531 0 .427-.349.75-.75.75-.192 0-.385-.073-.531-.219l-5.719-5.719-5.719 5.719c-.146.146-.339.219-.531.219-.401 0-.75-.323-.75-.75 0-.192.073-.384.22-.531l5.719-5.719-5.72-5.719c-.146-.147-.219-.339-.219-.532 0-.425.346-.749.75-.749.192 0 .385.073.531.219z</PathGeometry>
</Button>
</DockPanel>
<ContentPresenter/>
</DockPanel>
</Border> </Border>
</ControlTemplate> </ControlTemplate>
</Setter.Value> </Setter.Value>