前言
wpf中window相信大家都很熟悉,有时我们有一些自定义需求默认window是无法满足的,比如在标题栏上放一些自己东西,这个时候我们就需要写一个自己的window,实现起来也很简单,只要给window设置一个windowchrome.windowchrome附加属性就可以实现,windowchrome 可以让你自定义窗口的非工作区的外观和行为。非工作区就是窗口的标题栏和边框,通常由操作系统绘制和管理。windowchrome 可以让你将 wpf 的内容扩展到非工作区,同时保留一些系统的功能和行为,比如调整大小,移动,最大化,最小化等。
一、示例代码
1.1 基本使用
<local:customwindow x:class="customwindowdemo.window1" 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:local="clr-namespace:customwindowdemo" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" title="window1" width="800" height="450" icon="/logo.png" mc:ignorable="d"> <grid /> </local:customwindow>
1.2 自定义标题栏高度
<local:customwindow x:class="customwindowdemo.window1" 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:local="clr-namespace:customwindowdemo" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" title="window1" width="800" height="450" captionheight="35" icon="/logo.png" mc:ignorable="d"> <grid /> </local:customwindow>
1.3 自定义标题栏颜色
<local:customwindow x:class="customwindowdemo.window1" 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:local="clr-namespace:customwindowdemo" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" title="window1" width="800" height="450" captionbackground="blue" icon="/logo.png" mc:ignorable="d"> <grid /> </local:customwindow>
1.4 自定义标题栏内容
<local:customwindow x:class="customwindowdemo.window1" 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:local="clr-namespace:customwindowdemo" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" title="window1" width="800" height="450" icon="/logo.png" mc:ignorable="d"> <local:customwindow.captionbarcontent> <grid> <grid.columndefinitions> <columndefinition width="auto" /> <columndefinition width="*" /> <columndefinition width="auto" /> </grid.columndefinitions> <button margin="5" padding="2" verticalalignment="center" background="transparent" borderthickness="0" windowchrome.ishittestvisibleinchrome="true"> <stackpanel orientation="horizontal"> <polygon verticalalignment="center" fill="white" points="0,6 6,0 6,12" /> <textblock margin="4,0,0,0" verticalalignment="center" fontsize="14" foreground="white" text="返回" /> </stackpanel> </button> <textblock grid.column="1" horizontalalignment="center" verticalalignment="center" fontsize="14" foreground="white" text="{binding relativesource={relativesource ancestortype=window}, path=title}" /> <button grid.column="2" margin="5" padding="2" verticalalignment="center" background="transparent" borderthickness="0" fontsize="14" foreground="white" windowchrome.ishittestvisibleinchrome="true"> <stackpanel orientation="horizontal"> <textblock margin="0,0,4,0" verticalalignment="center" text="admin" /> <polyline verticalalignment="center" points="0,0 5,5 10,0" stroke="white" strokethickness="2" /> </stackpanel> </button> </grid> </local:customwindow.captionbarcontent> <grid /> </local:customwindow>
二、综合案例
到此这篇关于基于wpf封装一个可扩展的window的文章就介绍到这了,更多相关wpf封装可扩展window内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论