当前位置: 代码网 > it编程>编程语言>C# > 基于WPF封装一个可扩展的Window

基于WPF封装一个可扩展的Window

2024年05月15日 C# 我要评论
前言wpf中window相信大家都很熟悉,有时我们有一些自定义需求默认window是无法满足的,比如在标题栏上放一些自己东西,这个时候我们就需要写一个自己的window,实现起来也很简单,只要给win

前言

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内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com