经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
WPF如何封装一个可扩展的Window
来源:cnblogs  作者:趋时软件  时间:2024/4/1 10:27:10  对本文有异议

前言

    WPF中Window相信大家都很熟悉,有时我们有一些自定义需求默认Window是无法满足的,比如在标题栏上放一些自己东西,这个时候我们就需要写一个自己的Window,实现起来也很简单,只要给Window设置一个WindowChrome.WindowChrome附加属性就可以实现,WindowChrome 可以让你自定义窗口的非工作区的外观和行为。非工作区就是窗口的标题栏和边框,通常由操作系统绘制和管理。WindowChrome 可以让你将 WPF 的内容扩展到非工作区,同时保留一些系统的功能和行为,比如调整大小,移动,最大化,最小化等。

一、示例代码

1.1 基本使用

  1. <local:CustomWindow
  2. x:Class="CustomWindowDemo.Window1"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. xmlns:local="clr-namespace:CustomWindowDemo"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. Title="Window1"
  9. Width="800"
  10. Height="450"
  11. Icon="/logo.png"
  12. mc:Ignorable="d">
  13. <Grid />
  14. </local:CustomWindow>

 1.2 自定义标题栏高度

  1. <local:CustomWindow
  2. x:Class="CustomWindowDemo.Window1"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. xmlns:local="clr-namespace:CustomWindowDemo"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. Title="Window1"
  9. Width="800"
  10. Height="450"
  11. CaptionHeight="35"
  12. Icon="/logo.png"
  13. mc:Ignorable="d">
  14. <Grid />
  15. </local:CustomWindow>

1.3 自定义标题栏颜色

 

  1. <local:CustomWindow
  2. x:Class="CustomWindowDemo.Window1"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. xmlns:local="clr-namespace:CustomWindowDemo"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. Title="Window1"
  9. Width="800"
  10. Height="450"
  11. CaptionBackground="Blue"
  12. Icon="/logo.png"
  13. mc:Ignorable="d">
  14. <Grid />
  15. </local:CustomWindow>

1.4 自定义标题栏内容

  1. <local:CustomWindow
  2. x:Class="CustomWindowDemo.Window1"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. xmlns:local="clr-namespace:CustomWindowDemo"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. Title="Window1"
  9. Width="800"
  10. Height="450"
  11. Icon="/logo.png"
  12. mc:Ignorable="d">
  13. <local:CustomWindow.CaptionBarContent>
  14. <Grid>
  15. <Grid.ColumnDefinitions>
  16. <ColumnDefinition Width="Auto" />
  17. <ColumnDefinition Width="*" />
  18. <ColumnDefinition Width="Auto" />
  19. </Grid.ColumnDefinitions>
  20.  
  21. <Button
  22. Margin="5"
  23. Padding="2"
  24. VerticalAlignment="Center"
  25. Background="Transparent"
  26. BorderThickness="0"
  27. WindowChrome.IsHitTestVisibleInChrome="True">
  28. <StackPanel Orientation="Horizontal">
  29. <Polygon
  30. VerticalAlignment="Center"
  31. Fill="White"
  32. Points="0,6 6,0 6,12" />
  33. <TextBlock
  34. Margin="4,0,0,0"
  35. VerticalAlignment="Center"
  36. FontSize="14"
  37. Foreground="White"
  38. Text="返回" />
  39. </StackPanel>
  40. </Button>
  41. <TextBlock
  42. Grid.Column="1"
  43. HorizontalAlignment="Center"
  44. VerticalAlignment="Center"
  45. FontSize="14"
  46. Foreground="White"
  47. Text="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Title}" />
  48. <Button
  49. Grid.Column="2"
  50. Margin="5"
  51. Padding="2"
  52. VerticalAlignment="Center"
  53. Background="Transparent"
  54. BorderThickness="0"
  55. FontSize="14"
  56. Foreground="White"
  57. WindowChrome.IsHitTestVisibleInChrome="True">
  58. <StackPanel Orientation="Horizontal">
  59. <TextBlock
  60. Margin="0,0,4,0"
  61. VerticalAlignment="Center"
  62. Text="Admin" />
  63. <Polyline
  64. VerticalAlignment="Center"
  65. Points="0,0 5,5 10,0"
  66. Stroke="White"
  67. StrokeThickness="2" />
  68. </StackPanel>
  69. </Button>
  70. </Grid>
  71. </local:CustomWindow.CaptionBarContent>
  72. <Grid />
  73. </local:CustomWindow>

 二、综合案例

 

如需以上代码,请到群共享文件中下载

 

技术交流群
 
联系方式

原文链接:https://www.cnblogs.com/qushi2020/p/18106471

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号