基于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# WPF 建立无边框(标题栏)的登录窗口的示例

    C# WPF 建立无边框(标题栏)的登录窗口的示例

    这篇文章主要介绍了C# WPF 建立无边框(标题栏)的登录窗口的示例,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2020-12-12
  • Unity实现人物平滑转身

    Unity实现人物平滑转身

    这篇文章主要为大家详细介绍了Unity实现人物平滑转身,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • C#中的匿名方法实例解析

    C#中的匿名方法实例解析

    这篇文章主要介绍了C#中的匿名方法,包括其由来、定义及用法等,需要的朋友可以参考下
    2014-09-09
  • C# 设计模式系列教程-组合模式

    C# 设计模式系列教程-组合模式

    组合模式可以使客户端调用简单,它可以一致使用组合结构或是其中单个对象,简化了客户端代码。
    2016-06-06
  • C#设置窗体最大化且不遮挡任务栏的方法

    C#设置窗体最大化且不遮挡任务栏的方法

    这篇文章主要介绍了C#设置窗体最大化且不遮挡任务栏的方法,涉及针对form窗体的宽和高的相对大小操作,是非常简单而实用的技巧,需要的朋友可以参考下
    2014-12-12
  • C#设计模式之策略模式

    C#设计模式之策略模式

    这篇文章介绍了C#设计模式之策略模式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • C#使用Queue<T>进行队列设计

    C#使用Queue<T>进行队列设计

    Queue<T>类提供了许多方法和属性,用于处理队列中的元素,本文主要介绍了C#使用Queue<T>进行队列设计,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • C#文件流进行压缩和解压缩的方法

    C#文件流进行压缩和解压缩的方法

    这篇文章主要介绍了C#文件流进行压缩和解压缩的方法,涉及C#文件流操作的相关技巧,需要的朋友可以参考下
    2015-05-05
  • C#中TextBox的横线样式及占位提示详解

    C#中TextBox的横线样式及占位提示详解

    横线样式就是将TextBox以一条底横线的形式展示在页面,占位提示就是Web的Placeholder属性,即在输入框没有内容的时候进行一个输入提示。本文主要介绍了C#中TextBox的这两个的实现,需要的可以参考一下
    2022-11-11
  • WinForm实现最小化到系统托盘方法实例详解

    WinForm实现最小化到系统托盘方法实例详解

    这篇文章主要介绍了WinForm实现最小化到系统托盘方法,实例分析了C#中实现WinForm最小化到系统托盘所需的相关控件与使用技巧,需要的朋友可以参考下
    2015-05-05

最新评论