WPF如何封装一个可扩展的Window

前言

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>

二、综合案例

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

相关推荐
Mcband4 小时前
EasyExcel动态表头
java·windows
L_cl9 小时前
【NLP 75、如何通过API调用智谱大模型】
linux·服务器·windows
海尔辛9 小时前
学习黑客Active Directory 入门指南(一)
windows·学习·ad
※※冰馨※※9 小时前
彻底解决QT5 中文编译不过问题
c++·windows·qt
love530love9 小时前
【笔记】记一次PyCharm的问题反馈
ide·人工智能·windows·笔记·python·pycharm
范纹杉想快点毕业10 小时前
以项目的方式学QT开发C++(二)——超详细讲解(120000多字详细讲解,涵盖qt大量知识)逐步更新!
c语言·开发语言·c++·windows·vscode·qt·visual studio
兔子坨坨10 小时前
pycharm连接github(详细步骤)
windows·git·学习·pycharm·github
明耀13 小时前
WPF C# 用WebView加载H5页面(uniapp项目,vue项目)
uni-app·c#·wpf
Superstarimage14 小时前
使用conda创建python虚拟环境,并自定义路径
windows·python·conda
Evaporator Core20 小时前
深入探索:Core Web Vitals 进阶优化与新兴指标
前端·windows