wpf自定义标题栏系列

第一章 自定义标题栏
第二章 添加窗口阴影
第三章 style中定义标题栏
第四章 style使用参数及模板定义标题栏(本章)


文章目录

  • wpf自定义标题栏系列
  • 前言
  • 一、如何实现
    • 1、定义参数
    • 2、引用模板
    • 3、使用ContentPresenter
    • 4、添加拖动功能
    • 5、添加拖动调整大小功能
  • 二、完整代码
  • 三、使用示例
    • 1、使用方法
      • (1)、应用style
      • (2)、设置参数
    • 2、基础样式
    • 2.去除最大最小化按钮
    • 3.添加标题按钮
  • 总结

前言

上一章我们实现了在style中自定义标题栏,解决了复用性的问题。但是在实际使用中,还是存在一些问题的,比如有些界面需要放几个菜单在标题栏或者不需要最大化按钮等,就会变得很不灵活。这时候就需要对style进行进一步拓展了。


一、如何实现

1、定义参数

参考《C# wpf style中使用参数的方法》我们需要定义一些参数用于可定制化修改。可以根据使用场景灵活定义参数,标题栏高度、背景色等。比如定义如下两个参数:

<!--最小化按钮可见性-->
<Visibility x:Key="MinimizeButtonVisibility"  >Visible</Visibility>
<!--最大化按钮可见性-->
<Visibility x:Key="MaximizeButtonVisibility"  >Visible</Visibility>
<!--标题栏模板-->
<ControlTemplate x:Key="WindowStyle_PART_Title" TargetType="ContentControl"/>

2、引用模板

在Wndow的style中的template中再次引用一个模板,模板名称是自定义的参数名,如下是WindowStyle_PART_Title

 <!--标题栏-->
<ContentControl VerticalAlignment="Top"  Template="{DynamicResource WindowStyle_PART_Title}"><Grid><!--最小化按钮--><Button Visibility="{DynamicResource MinimizeButtonVisibility}" /><!--最大化按钮--><Button Visibility="{DynamicResource MaximizeButtonVisibility}" /><!--关闭按钮--><Button /></Grid>
</ContentControl>

3、使用ContentPresenter

这里的并非上一章的代表客户区的ContentPresenter。而是代表标题栏的ContentPresenter。在style应用的窗口定义一个key为WindowStyle_PART_Title的模板,在模板中使用ContentPresenter就代表标题栏。

<ControlTemplate x:Key="WindowStyle_PART_Title" TargetType="ContentControl"><Grid ><!--此处代表style中的标题栏--><ContentPresenter Content="{TemplateBinding Content}"></ContentPresenter><TextBlock   Text="标题名称"  Foreground="#d8d8d8" FontSize="18"  HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock></Grid>
</ControlTemplate>

4、添加拖动功能

拖动功能参考《C# wpf 附加属性实现任意控件(包括窗口)拖动》,在Window的style中添加:

<Setter Property="local:Move.IsDragMoveable" Value="True" />

5、添加拖动调整大小功能

拖动功能参考《C# wpf 附加属性实现任意控件拖动调整大小》,在Window的style中添加:

<Setter Property="local:Resize.IsResizeable" Value="True" />

二、完整代码

https://download.csdn.net/download/u013113678/86111797


三、使用示例

1、使用方法

(1)、应用style

在Wndow的resources中引用style文件,动态绑定style即可。

<Window  Style="{DynamicResource WindowStyle_Normal_Gray}"  />

(2)、设置参数

在Wndow的resources中参数定义

  <!--标题栏模板--><ControlTemplate x:Key="WindowStyle_PART_Title" TargetType="ContentControl"><!--ContentPresenter 代表style中定义的关闭最大最小化按钮--><ContentPresenter Content="{TemplateBinding Content}"></ContentPresenter></ControlTemplate><!--最小化按钮可见性--><Visibility x:Key="MinimizeButtonVisibility"  >Collapsed</Visibility><!--最大化按钮可见性--><Visibility x:Key="MaximizeButtonVisibility"  >Collapsed</Visibility>

2、基础样式

<Window x:Class="WpfApp6.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:WpfApp6"        Style="{DynamicResource WindowStyle_Normal_Gray}"  mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="WindowStyles.xaml" /></ResourceDictionary.MergedDictionaries></ResourceDictionary></Window.Resources><!--客户区--><Border   CornerRadius="10"  Background="#1e1e1e"></Border>
</Window>

效果预览:

2.去除最大最小化按钮

<Window x:Class="WpfApp6.UrlWindow"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:WpfApp6"mc:Ignorable="d"Title="UrlWindow"  Width="500" Height="300"Style="{DynamicResource WindowStyle_Normal_Gray}"   WindowStartupLocation="CenterOwner"><Window.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="/WindowStyles.xaml" /></ResourceDictionary.MergedDictionaries><!--最小化按钮可见性--><Visibility x:Key="MinimizeButtonVisibility"  >Collapsed</Visibility><!--最大化按钮可见性--><Visibility x:Key="MaximizeButtonVisibility"  >Collapsed</Visibility></ResourceDictionary></Window.Resources><Grid><!--客户区内容略--></Grid>
</Window>

效果预览(客户区内容未在xaml代码中展示)

3.添加标题按钮

<Window x:Class="WpfApp6.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:WpfApp6"        Style="{DynamicResource WindowStyle_Normal_Gray}"  mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="WindowStyles.xaml" /></ResourceDictionary.MergedDictionaries><!--标题栏模板--><ControlTemplate x:Key="WindowStyle_PART_Title" TargetType="ContentControl"><Border x:Name="bd_caption"  Height="50" Background="#333333"  CornerRadius="10,10,0,0"   ><Grid ><!--ContentPresenter 代表style中定义的关闭最大最小化按钮--><ContentPresenter Content="{TemplateBinding Content}"></ContentPresenter><StackPanel  Margin="10,0,0,0" Orientation="Horizontal"><!--LOGO--><TextBlock  HorizontalAlignment="Left"  VerticalAlignment="Center"  Text="AC" FontWeight="Bold"  Foreground="#d8d8d8"  FontSize="18"   /><!--按钮--><Button Margin="20,0,0,0"  HorizontalAlignment="Left"  Width="24" Height="24" Cursor="Hand"  Focusable="False"  Click="Button_Click"><Button.Template><ControlTemplate TargetType="Button" ><Image Source="link@1x.png"  Width="24" Height="24" /></ControlTemplate></Button.Template></Button></StackPanel><TextBlock   Text="自定义标题栏示例"  Foreground="#d8d8d8" FontSize="18"  HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock></Grid></Border></ControlTemplate></ResourceDictionary></Window.Resources><!--客户区--><Border   CornerRadius="10"  Background="#1e1e1e"></Border>
</Window>

效果预览:

动态效果


总结

以上就是今天要讲的内容,本文介绍的是对自定义窗口无边框窗口的细化实现,达到了很好的界面效果以及灵活性同时功能也是完备的,最大化、最小化、关闭、拖动、拖动调整大小都在style实现了。使用的时候只需要灵活调整样式即可。

C# wpf style中实现可定制的自定义标题栏相关推荐

  1. [WPF]在Style中设置ToolTip的问题分析

    刚才开到智者千虑发的[WPF]在Style中设置ToolTip的问题的博文,虽然最终给了一个暂时解决问题的方案,但是没有分析和解释其中的问题,正与他所说:但至于为什么不能直接在Setter.Value ...

  2. 在WPF TreeView中使用复选框

    目录 介绍 背景 细节决定成败 功能要求 将Smarts放入ViewModel 树视图配置 将TreeViewItem变成ToggleButton Aero主题中的复选框错误 介绍 本文回顾了一个WP ...

  3. WPF/Silverlight中MVVM运用

    随着WPF/Silverlight等技术的出现,一种新的模式出现在大家面前,那就是MVVM,提到这个模式,大家也许感觉很迷惑,也许会不屑于故,也许你会说我现在做项目都形成了自己的框架了,为什么还要花费 ...

  4. Prism.Wpf框架中WindowStartupLocation的问题

    目录 一.出现场景 二.原因分析 三.解决方案 一.出现场景 版本:Prism.Wpf 8.1.97.在应用IDialogService,来显示弹窗时,主界面的ViewModel: public cl ...

  5. WPF xaml中列表依赖属性的定义

    原文:WPF xaml中列表依赖属性的定义 列表内容属性 如上图,是一个列表标题排序控件,我们需要定义一个标题列表,从而让调用方可以自由的设置标题信息. 在自定义控件时,会遇到列表依赖属性,那么该如何 ...

  6. WPF程序中的XPSDocumentViewer

    在.NET 3.0中,提供了对XPS文件格式的全新支持.在WPF程序中,有一个控件是DocumentViewer.这里对它的使用做一个简要介绍 <Window x:Class="Wpf ...

  7. WPF 3D中多个模型如何设置某一个在最前?

    原文:WPF 3D中多个模型如何设置某一个在最前? 问题:我们的模型包括导入的3D solid模型和axis坐标轴模型,当模型旋转的时候,3D会将axis挡住. 期望:axis一直在最前面,不会被3D ...

  8. style 里引用php变量,在VUE style中使用data中的变量的方法详解

    最近项目中的公共组件,在复用的时候,针对不同的场景,需要不断变更CSS里样式的值,而且已经有了全局的公共组件样式了 如果用vue传统的动态绑定class和style的方式去修改样式(文末会提到),需要 ...

  9. 解决WPF程序中ListBox ItemsSource变化时不重置ScrollBar的问题

    解决WPF程序中ListBox ItemsSource变化时不重置ScrollBar的问题 参考文章: (1)解决WPF程序中ListBox ItemsSource变化时不重置ScrollBar的问题 ...

最新文章

  1. leetcode算法题--旋转数组的最小数字
  2. Web 前端技术图谱-菜鸟教程
  3. Fragment使用PagerSlidingTabStrip嵌套子Fragment显示问题
  4. linux系统备份路径,linux系统备份恢复到本机或是别的机器上
  5. linux 安装python_Linux/Mac/Windows的Rstudio安装Python模块总报错,怎么破?
  6. spark mllib推荐算法使用
  7. [HAOI2015]T2
  8. Centos 7 RabbitMQ + Haproxy 集群高可用部署
  9. Linux数据链路层的包解析
  10. socket与http的区别
  11. 计算机端最好用的词典——GoldenDict
  12. 哇嘎显示等待无服务器,vagaa 哇嘎搜索不了资源怎么破?vagaa 哇嘎无法搜索的原因分析和解决方法介绍...
  13. 【晶体管电路设计】五、渥尔曼电路设计
  14. Windows下Netron安装
  15. 更换win10计算机账户,win10更换账户的方法是什么_win10换账号登录的方法
  16. 一个网工的十年奋斗史 - 工作篇
  17. Angular Router的组件路由介绍
  18. 学习ELMo从文本中提取特征的分步NLP指南
  19. Android使用GoogleMap实现定位及定位回正
  20. 【电源专题】线性稳压器基础(线性稳压器是哪里线性了?)

热门文章

  1. 三个角色,十五年,二十万选手:我们如何理解“百度之星”?
  2. 深圳周末好去处|深圳一日游推荐攻略
  3. 深圳发布共享单车规范,将对不良用户行为采取特别措施
  4. 机器人点灯(light-bot)2.0通关攻略(一)——递归
  5. CentOS编译安装subversion 1.9.7
  6. ppt模板下载keyppt.cn
  7. Neo4j CQL-Merge(合并)
  8. 最新kali之pixiewps
  9. 有监督算法和无监督算法的理解
  10. javase加强(七、 不可变集合、Stream、异常)