添加自定义窗口样式

This commit is contained in:
于智纯
2025-09-20 22:02:26 +08:00
parent 437c2b1252
commit ea3a6ad9ea
7 changed files with 171 additions and 42 deletions

View File

@@ -1,9 +1,154 @@
<Application x:Class="Deedy.Testing.App"
<Application x:Class="Deedy.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Deedy.Testing"
xmlns:local="clr-namespace:Deedy"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<!--Boolean到Visibility的转换-->
<BooleanToVisibilityConverter x:Key="bool2VisibilityConverter"/>
<!--Boolean【取反】后到Visibility的转换-->
<!--<local:BoolInverterToVisibilityConverter x:Key="boolInverter2VisiblityConverter"/>-->
<!--Boolean【取反】一般用于IsReadonly属性-->
<!--<local:BoolInverterConverter x:Key="boolInverterConverter"/>-->
<!--【验权】后再对Boolean进行【与】运算-->
<!--<local:BoolWithPermissionConverter x:Key="boolWithPermissionConverter"/>-->
<!--【验权】后取反再与原始值进行【或】运算一般用于IsReadonly属性-->
<!--<local:BoolInverterWithPermissionConverter x:Key="boolInverterWithPermissionConverter"/>-->
<!--【验权】后与原始值进行【与】运算再转换为Visibility实例-->
<!--<local:BoolToVisibilityWithPermissionConverter x:Key="boolToVisibilityWithPermissionConverter"/>-->
<GridLength x:Key="LeftFrame">400</GridLength>
<!--绅士风:黑灰色主题风格-->
<Style TargetType="FrameworkElement">
<Setter Property="Focusable" Value="False"/>
</Style>
<!--#region 常规背景色-->
<!--高透明背景色-->
<SolidColorBrush x:Key="BGHyaline" Color="#01000000" Opacity="0.01"/>
<!--标题栏背景色-->
<SolidColorBrush x:Key="BGTitle" Color="#FF505050"/>
<!--菜单栏背景色-->
<SolidColorBrush x:Key="BGMenu" Color="#FF606060"/>
<!--工具栏背景色-->
<SolidColorBrush x:Key="BGTool" Color="#FF686868"/>
<!--区域头背景色-->
<SolidColorBrush x:Key="BGHeader" Color="#FF707070"/>
<!--奇数行背景色-->
<SolidColorBrush x:Key="BGOddRow" Color="#FF808080"/>
<!--偶数行背景色-->
<SolidColorBrush x:Key="BGEvenRow" Color="#FF989898"/>
<!--普通的背景色-->
<SolidColorBrush x:Key="BGNormal" Color="#FFA8A8A8"/>
<!--悬停项背景色-->
<SolidColorBrush x:Key="BGHover" Color="#FFB0B0B0"/>
<!--选中项背景色-->
<SolidColorBrush x:Key="BGSelected" Color="#FF505050"/>
<!--禁用项背景色-->
<SolidColorBrush x:Key="BGDisable" Color="#FFC0C0C0"/>
<!--#endregion-->
<!--#region 常规前景色-->
<!--标题文本颜色-->
<SolidColorBrush x:Key="FGTitle" Color="#FFFFFFFF"/>
<!--表头文本颜色-->
<SolidColorBrush x:Key="FGHeader" Color="#FFF8F8F8"/>
<!--标签文本颜色-->
<SolidColorBrush x:Key="FGLable" Color="#FFF0F0F0"/>
<!--内容文本颜色-->
<SolidColorBrush x:Key="FGContent" Color="#FFE0E0E0"/>
<!--选中项前景色-->
<SolidColorBrush x:Key="FGSelected" Color="#FFD0D0D0"/>
<!--水印文本颜色-->
<SolidColorBrush x:Key="FGWatermark" Color="#FFD8D8D8"/>
<!--禁用项前景色-->
<SolidColorBrush x:Key="FGDisable" Color="#FF808080"/>
<!--链接文本颜色-->
<SolidColorBrush x:Key="FGLink" Color="#FFDDEEFF"/>
<!--重点文本颜色-->
<SolidColorBrush x:Key="FGKeynote" Color="#FFFFCC99"/>
<!--高亮文本颜色-->
<SolidColorBrush x:Key="FGHighlight" Color="#FFFFFF00"/>
<!--#endregion-->
<!--#region 常规边框色-->
<!--区域边框线色-->
<SolidColorBrush x:Key="BBBorder" Color="#FFFFFFFF"/>
<!--内容分隔线色-->
<SolidColorBrush x:Key="BBSeparator" Color="#FF808080"/>
<!--高亮边框线色-->
<SolidColorBrush x:Key="BBHighlight" Color="#FFFF8800"/>
<!--区域虚线边框-->
<VisualBrush x:Key="DLBBorder">
<VisualBrush.Visual>
<Rectangle StrokeDashArray="4 2"
Stroke="#FFFFFFFF"
StrokeThickness="{Binding StrokeThickness, RelativeSource={RelativeSource AncestorType={x:Type Border}}}"
Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Border}}}"
Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type Border}}}"/>
</VisualBrush.Visual>
</VisualBrush>
<!--内容虚线分隔-->
<VisualBrush x:Key="DLSBorder">
<VisualBrush.Visual>
<Rectangle StrokeDashArray="4 2"
Stroke="#FF808080"
StrokeThickness="{Binding StrokeThickness, RelativeSource={RelativeSource AncestorType={x:Type Border}}}"
Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Border}}}"
Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type Border}}}"/>
</VisualBrush.Visual>
</VisualBrush>
<!--高亮虚线边框-->
<VisualBrush x:Key="DLHBorder">
<VisualBrush.Visual>
<Rectangle StrokeDashArray="4 2"
Stroke="#FFFF8800"
StrokeThickness="{Binding StrokeThickness, RelativeSource={RelativeSource AncestorType={x:Type Border}}}"
Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Border}}}"
Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type Border}}}"/>
</VisualBrush.Visual>
</VisualBrush>
<!--#endregion-->
<!--#region 滚动条背景-->
<!--滚动条轨道背景-->
<SolidColorBrush x:Key="ScrBGTrack" Color="#FFA8A8A8"/>
<!--滚动条按钮背景-->
<SolidColorBrush x:Key="ScrBGButton" Color="#FFB0B0B0"/>
<!--滚动条滑块背景-->
<SolidColorBrush x:Key="ScrBGThumb" Color="#FF777777"/>
<!--#endregion-->
<!--#region 按钮调色板-->
<!--选中按钮背景-->
<SolidColorBrush x:Key="ButBGChecked" Color="#FF606060"/>
<!--常规按钮背景-->
<SolidColorBrush x:Key="ButBGNormal" Color="#FF787878"/>
<!--悬停按钮背景-->
<SolidColorBrush x:Key="ButBGHover" Color="#FF909090"/>
<!--禁用按钮背景-->
<SolidColorBrush x:Key="ButBGDisable" Color="#FFA8A8A8"/>
<!--常规按钮文本-->
<SolidColorBrush x:Key="ButFGNormal" Color="#FFFFFFFF"/>
<!--高亮按钮文本-->
<SolidColorBrush x:Key="ButFGHighlight" Color="#FF00FFFF"/>
<!--悬停按钮文本-->
<SolidColorBrush x:Key="ButFGHover" Color="#FF00FF88"/>
<!--选中按钮文本-->
<SolidColorBrush x:Key="ButFGChecked" Color="#FFFFBB00"/>
<!--危险按钮文本-->
<SolidColorBrush x:Key="ButFGCareful" Color="#FFFF8888"/>
<!--默认按钮文本-->
<SolidColorBrush x:Key="ButFGDefault" Color="#FF00CC88"/>
<!--取消按钮文本-->
<SolidColorBrush x:Key="ButFGCancel" Color="#FFE0E0E0"/>
<!--禁用按钮文本-->
<SolidColorBrush x:Key="ButFGDisable" Color="#FFD8D8D8"/>
<!--#endregion-->
</ResourceDictionary>
</Application.Resources>
</Application>

View File

@@ -2,7 +2,7 @@
using System.Data;
using System.Windows;
namespace Deedy.Testing
namespace Deedy
{
/// <summary>
/// Interaction logic for App.xaml

View File

@@ -13,7 +13,7 @@ using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Deedy.Testing
namespace Deedy
{
/// <summary>
/// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
@@ -22,14 +22,14 @@ namespace Deedy.Testing
/// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
/// 元素中:
///
/// xmlns:MyNamespace="clr-namespace:Deedy.Testing"
/// xmlns:MyNamespace="clr-namespace:Deedy"
///
///
/// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
/// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
/// 元素中:
///
/// xmlns:MyNamespace="clr-namespace:Deedy.Testing;assembly=Deedy.Testing"
/// xmlns:MyNamespace="clr-namespace:Deedy;assembly=Deedy"
///
/// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
/// 并重新生成以避免编译错误:
@@ -44,6 +44,9 @@ namespace Deedy.Testing
/// <MyNamespace:ElegantWindow/>
///
/// </summary>
[TemplatePart(Name = "PART_MinButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_MaxButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_CloseButton", Type = typeof(Button))]
public class ElegantWindow : Window
{
static ElegantWindow()
@@ -52,6 +55,7 @@ namespace Deedy.Testing
}
public ElegantWindow() : base()
{
}
public override void OnApplyTemplate()
{

View File

@@ -53,34 +53,5 @@ namespace Deedy
else throw new InvalidOperationException("窗体装饰器[WindowDecorator]对象只能附加到作为标题栏容器的[Decorator]类型派生对象(例如[Border]对象)上!");
}
}));
public static string GetTextBoxRegexMatch(DependencyObject obj)
{
return (string)obj.GetValue(TextBoxRegexMatchProperty);
}
public static void SetTextBoxRegexMatch(DependencyObject obj, string value)
{
obj.SetValue(TextBoxRegexMatchProperty, value);
}
// Using a DependencyProperty as the backing store for TextBoxRegexMatch. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextBoxRegexMatchProperty =
DependencyProperty.RegisterAttached("TextBoxRegexMatch", typeof(string), typeof(ExMarkup), new PropertyMetadata("", (d, e) =>
{
if (d is TextBox textBox)
{
if (!string.IsNullOrEmpty(e.NewValue as string) && string.IsNullOrEmpty(e.OldValue as string))
{
//加事件监听逻辑
}
if (string.IsNullOrEmpty(e.NewValue as string) && !string.IsNullOrEmpty(e.OldValue as string))
{
//取消事件监听逻辑
}
}
}));
}
}

View File

@@ -1,12 +1,21 @@
<Window x:Class="Deedy.Testing.MainWindow"
<Window x:Class="Deedy.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Deedy.Testing"
xmlns:local="clr-namespace:Deedy"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Closing="Window_Closing">
<DockPanel>
<Border Height="40" DockPanel.Dock="Top" >
<local:ExMarkup.WindowChrome>
<local:WindowDecorator Background="{DynamicResource BGTitle}" Foreground="{DynamicResource FGTitle}" FontSize="20" HoverBrush="{DynamicResource FGKeynote}">
</local:WindowDecorator>
</local:ExMarkup.WindowChrome>
</Border>
<Grid>
</Grid>
</DockPanel>
</Window>

View File

@@ -9,7 +9,7 @@ using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Deedy.Testing
namespace Deedy
{
/// <summary>
/// Interaction logic for MainWindow.xaml

View File

@@ -1,7 +1,7 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Deedy.Testing"
xmlns:local="clr-namespace:Deedy"
xmlns:deedy="clr-namespace:Deedy">
<Style TargetType="{x:Type deedy:WindowDecorator}">