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

 更新时间:2024年04月02日 16:54:19   作者:趋时软件  
WPF中Window相信大家都很熟悉,有时我们有一些自定义需求默认Window是无法满足的,所以本文就来和大家聊聊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>

 二、综合案例

到此这篇关于基于WPF封装一个可扩展的Window的文章就介绍到这了,更多相关WPF封装可扩展Window内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C# 和 Python 的 hash_md5加密方法

    C# 和 Python 的 hash_md5加密方法

    这篇文章主要介绍了C# 和 Python 的 hash_md5加密方法,文章围绕着C# 和 Python 的 hash_md5加密的相关资料展开文章的详细呢偶然,需要的朋友可以参考一下,希望对你有所帮助
    2021-11-11
  • 一个可携带附加消息的增强消息框MessageBoxEx

    一个可携带附加消息的增强消息框MessageBoxEx

    一个可携带附加消息的增强消息框MessageBoxEx分享给大家,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • C#控制反转的使用详解

    C#控制反转的使用详解

    控制反转是将对象的创建、依赖管理和生命周期控制从应用程序代码中转移出来,交由外部容器来管理,下面就来详细的介绍一下C#控制反转的使用,感兴趣的可以了解一下
    2026-01-01
  • C#中删除字符串指定位置字符的5种方法

    C#中删除字符串指定位置字符的5种方法

    文章介绍了在C#中删除字符串中第x个字符的几种方法,包括String.Remove()、Substring()、StringBuilder、字符数组和LINQ,并提供了完整示例,推荐使用String.Remove()方法,因其简洁高效,下面小编通过代码为大家详细说说,需要的朋友可以参考下
    2026-02-02
  • C# 使用鼠标点击对Chart控件实现数据提示效果

    C# 使用鼠标点击对Chart控件实现数据提示效果

    这篇文章主要介绍了C# 使用鼠标点击对Chart控件实现数据提示效果,文章给予上一篇的详细内容做延伸介绍,需要的小伙伴可任意参考一下
    2022-08-08
  • 如何用C#找出数组中只出现了一次的数字

    如何用C#找出数组中只出现了一次的数字

    数组从字面上理解就是存放一组数,下面这篇文章主要给大家介绍了关于如何用C#找出数组中只出现了一次的数字,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-12-12
  • 详解C# 线程的挂起与唤醒

    详解C# 线程的挂起与唤醒

    这篇文章主要介绍了详解C# 线程的挂起与唤醒,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-05-05
  • C# 10分钟完成百度人脸识别(入门篇)

    C# 10分钟完成百度人脸识别(入门篇)

    这篇文章主要介绍了C# 10分钟完成百度人脸识别(入门篇),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-02-02
  • DevExpress实现为TextEdit设置水印文字的方法

    DevExpress实现为TextEdit设置水印文字的方法

    这篇文章主要介绍了DevExpress实现为TextEdit设置水印文字的方法,对C#程序设计人员来说是一个很实用的技巧,需要的朋友可以参考下
    2014-08-08
  • C#操作ini文件的帮助类

    C#操作ini文件的帮助类

    这篇文章介绍了C#操作ini文件的帮助类,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04

最新评论