1、废话不多说直接上代码

<Window.Resources>
<Style x:Key="btnTitleMaxMin" TargetType="Button">
<Setter Property="Width" Value="30"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="Cursor" Value="Hand"></Setter>
<Setter Property="Margin" Value="1,0,1,0"></Setter>
<Setter Property="Foreground" Value="DarkRed"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="columnDefinition1" Width="0.982*"/>
<ColumnDefinition x:Name="columnDefinition" Width="0.018*"/>
</Grid.ColumnDefinitions>
<Border x:Name="rectangle" Background="Transparent" BorderThickness="0" CornerRadius="0" Grid.ColumnSpan="2"></Border>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="rectangle" Value="RoyalBlue"></Setter>
<Setter Property="Foreground" Value="white"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Transparent"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="btnTitleClose" TargetType="Button">
<Setter Property="Width" Value="35"></Setter>
<Setter Property="Height" Value="35"></Setter>
<Setter Property="Cursor" Value="Hand"></Setter>
<Setter Property="Margin" Value="1,0,1,0"></Setter>
<Setter Property="Foreground" Value="DarkRed"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="columnDefinition1" Width="0.982*"/>
<ColumnDefinition x:Name="columnDefinition" Width="0.018*"/>
</Grid.ColumnDefinitions>
<Border x:Name="rectangle" Background="Transparent" CornerRadius="0,4,0,0" Grid.ColumnSpan="2"></Border>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="rectangle" Value="Firebrick"></Setter>
<Setter Property="Foreground" Value="white"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Transparent"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="CustomWindowTemplate" TargetType="Window">
<Border BorderBrush="#FF4949" CornerRadius="4">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
</Grid.RowDefinitions>
<Border Background="#FF4949" BorderBrush="Transparent" MouseDown="Grid_MouseDown" CornerRadius="1,1,0,0">
<Grid Name="Title" Margin="0">
<TextBlock HorizontalAlignment="Left" Margin="10,0,0,0" Text="{TemplateBinding Title}" VerticalAlignment="Center"></TextBlock>
<Button ToolTip="最小化" FontSize="20" Content="0" FontFamily="Webdings" Name="btnMin" Click="btnMin_Click" HorizontalAlignment="Right" Foreground="White" Margin="0,-13,42,0" Height="26" Width="26" Style="{DynamicResource btnTitleMaxMin}" VerticalAlignment="Center" />
<Button ToolTip="最大化" FontSize="20" Content="1" FontFamily="Webdings" Name="btnMaxOrMin" Click="btnMaxOrMin_Click" HorizontalAlignment="Right" Foreground="White" Margin="0,-9,21,0" Height="26" Width="26" Style="{DynamicResource btnTitleMaxMin}" VerticalAlignment="Center" />
<Button ToolTip="关闭" FontSize="20" Content="r" FontFamily="Webdings" Name="PART_CLOSEBTN" HorizontalAlignment="Right" Foreground="White" Margin="0,-9,1,0" Height="26" Width="26" Style="{DynamicResource btnTitleClose}" VerticalAlignment="Center" Click="Button_Click" />
</Grid>
</Border>
<Grid Grid.Row="1" Background="White" >
<Grid Margin="5,5,5,0">
<AdornerDecorator>
<ContentPresenter></ContentPresenter>
</AdornerDecorator>
</Grid>
</Grid>
<Grid Grid.Row="2" Background="White">
<ResizeGrip Name="WindowResizeGrip" Visibility="Collapsed" IsTabStop="False" HorizontalAlignment="Right" FontWeight="Black" VerticalAlignment="Bottom">
</ResizeGrip>
</Grid>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="ResizeMode" Value="CanResizeWithGrip">
<Setter TargetName="WindowResizeGrip" Property="Visibility" Value="Visible"></Setter>
</Trigger>
<Trigger Property="WindowState" Value="Maximized">
<Setter TargetName="btnMaxOrMin" Property="Content" Value="2"></Setter>
<Setter TargetName="btnMaxOrMin" Property="ToolTip" Value="还原"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>

最后在绑定Template="{DynamicResource CustomWindowTemplate}"

WPF标题栏自定义的更多相关文章

  1. WPF 自定义标题栏 自定义菜单栏

    自定义标题栏 自定义列表,可以直接修改WPF中的ListBox模板,也用这样类似的效果.但是ListBox是不能设置默认选中状态的. 而我们需要一些复杂的UI效果,还是直接自定义控件来的快 GitHu ...

  2. WPF 之 自定义窗体标题栏

    在WPF中自定义窗体标题栏,首先需要将窗体的WindowStyle属性设置为None,隐藏掉WPF窗体的自带标题栏.然后可以在窗体内部自定义一个标题栏. 例如,标题栏如下: <WrapPanel ...

  3. WPF绘制自定义窗口

    原文:WPF绘制自定义窗口 WPF是制作界面的一大利器,下面就用WPF模拟一下360的软件管理界面,360软件管理界面如下: 界面不难,主要有如下几个要素: 窗体的圆角 自定义标题栏及按钮 自定义状态 ...

  4. 在WPF中自定义你的绘制(五)

    原文:在WPF中自定义你的绘制(五) 在WPF中自定义你的绘制(五)                                                                   ...

  5. 在WPF中自定义你的绘制(三)

    原文:在WPF中自定义你的绘制(三) 在WPF中自定义你的绘制(三)                                                                  ...

  6. 在WPF中自定义你的绘制(四)

    原文:在WPF中自定义你的绘制(四)                                   在WPF中自定义你的绘制(四)                                 ...

  7. 在WPF中自定义你的绘制(一)

    原文:在WPF中自定义你的绘制(一)   在WPF中自定义你的绘制(一)                                                                 ...

  8. 在WPF中自定义你的绘制(二)

    原文:在WPF中自定义你的绘制(二)   在WPF中自定义你的绘制(二)                                                                 ...

  9. WPF 标题栏 右键窗口标题添加关于对话框

    /// <summary> /// wpf标题栏 右键菜单 中添加新项 /// </summary> public partial class MainWindow : Win ...

  10. 在VS2005中设置WPF中自定义按钮的事件

    原文:在VS2005中设置WPF中自定义按钮的事件 上篇讲了如何在Blend中绘制圆角矩形(http://blog.csdn.net/johnsuna/archive/2007/08/13/17407 ...

随机推荐

  1. Netty-架构设计及入门程序-3

    一.原生 NIO 存在的问题 1.NIO 的类库和 API 繁杂,使用麻烦:需要熟练掌握 Selector.ServerSocketChannel.SocketChannel.ByteBuffer等. ...

  2. 《Effective C++》资源管理章节

    Item 13:以对象管理资源 关键的两个想法(这种方式其实在很多地方都可以看出影子,比如managing pool的模型): 1.获得资源后立刻放入管理对象(managing object):以对象 ...

  3. CF构造题1600-1800(2)

    H. Hot Black Hot White(COMPFEST 14 - Preliminary Online Mirror (Unrated, ICPC Rules, Teams Preferred ...

  4. 大公司为什么禁止SpringBoot项目使用Tomcat?

    本文已经收录到Github仓库,该仓库包含计算机基础.Java基础.多线程.JVM.数据库.Redis.Spring.Mybatis.SpringMVC.SpringBoot.分布式.微服务.设计模式 ...

  5. Flutter 3.7 新特性:介绍后台isolate通道

    Flutter 3.7 发布,本人对其中后台 isolate 通道比较感兴趣,迫不及待翻译了下Aaron Clarke文章,第一次翻译,有不足地方欢迎各位大佬们评论区指正,我将持续更新到本文,谢谢. ...

  6. Redis 正则扫描key并删除

    扫描key /** * @param key * @return * @Description: 通过Scan的方式迭代key */ public Set<String> scanKeys ...

  7. DateFormat类&SimpleDateFrormat类介绍-Dateformat类的format方法parse方法

    DateFormat类&SimpleDateFrormat类介绍 java.text.DateFormat是日期/时间格式化子类的抽象类,我们通过这个类可以帮我们完成日期和文本之间的转换,也就 ...

  8. 面试必问:说一下 Java 虚拟机的内存布局?

    我们通常所说的 Java 虚拟机(JVM)的内存布局,一般是指 Java 虚拟机的运行时数据区(Runtime Data Area),也就是当字节码被类加载器加载之后的执行区域划分.当然它通常是 JV ...

  9. python学习第三周总结

    文件操作 文件的读写模式 文件的操作模式 文件相关操作 文件内光标移动 文件内容修改 函数前戏 函数的语法结构 函数的定义和调用 函数的分类 函数的返回值 函数的参数 函数参数之位置参数 默认参数 可 ...

  10. 破解练习-CRACKME002

    002-注册算法分析 一.工具和调试环境 动态调试工具:x64dbg 系统环境:win10 1909 二.分析Serial/name的算法 直接使用提示字符串验证,得到错误提示如下: 接下来使用x64 ...