WPF 使用 WindowChrome自定义Window窗体样式同时最大程度还原原生窗口样式

WindowEx.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:IFToolsControls.Controls">
    <!--
        https://www.cnblogs.com/webenh/p/17001190.html
        https://www.cnblogs.com/s0611163/p/9994529.html
        https://www.cnblogs.com/walterlv/p/10236439.html
        https://blog.youkuaiyun.com/qq_43562262/article/details/133244427
    -->
    <Style TargetType="{x:Type local:WindowEx}">
        <Style.Resources>
            <FontFamily x:Key="IconFont">pack://application:,,,/IFToolsControls;component/Assets/Fonts/#IFToolsControls</FontFamily>
            <ControlTemplate x:Key="CloseButtonTemplate" TargetType="Button">
                <Border
                    x:Name="border"
                    BorderBrush="{Binding TitleButtonBorderBrush, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:WindowEx}}}"
                    CornerRadius="0 0 5 0"
                    SnapsToDevicePixels="true">
                    <TextBlock
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        FontFamily="{StaticResource IconFont}"
                        Text="&#xe899;" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="border" Property="Background" Value="red" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
            <ControlTemplate x:Key="OnlyCloseButtonTemplate" TargetType="Button">
                <Border
                    x:Name="border"
                    BorderBrush="{Binding TitleButtonBorderBrush, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:WindowEx}}}"
                    CornerRadius="0 0 5 5"
                    SnapsToDevicePixels="true">
                    <TextBlock
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        FontFamily="{StaticResource IconFont}"
                        Text="&#xe899;" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="border" Property="Background" Value="red" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Style.Resources>
        <Setter Property="AllowsTransparency" Value="True" />
        <Setter Property="WindowStyle" Value="None" />
        <Setter Property="Title" Value="MainWindow" />
        <Setter Property="MinHeight" Value="35" />
        <Setter Property="MinWidth" Value="119" />
        <Setter Property="Icon" Value="pack://application:,,,/IFToolsControls;component/Assets/Images/WindowDefaultIcon.png" />
        <Setter Property="WindowChrome.WindowChrome">
            <Setter.Value>
                <WindowChrome
                    CaptionHeight="30"
                    CornerRadius="5"
                    GlassFrameThickness="0"
                    NonClientFrameEdges="None"
                    ResizeBorderThickness="2"
                    UseAeroCaptionButtons="False" />
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:WindowEx}">
                    <Border
                        x:Name="PART_RootBorder"
                        Background="{Binding TitleBackground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:WindowEx}}}"
                        CornerRadius="5">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="30" />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <Grid Grid.Row="0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="auto" />
                                    <ColumnDefinition />
                                    <ColumnDefinition Width="auto" />
                                </Grid.ColumnDefinitions>
                                <Image
                                    x:Name="PART_IconImage"
                                    Width="16"
                                    Height="16"
                                    Margin="10,0,2,0"
                                    Source="{TemplateBinding Icon}"
                                    WindowChrome.IsHitTestVisibleInChrome="True" />
                                <TextBlock
                                    Grid.Column="1"
                                    Margin="2,0,0,0"
                                    VerticalAlignment="Center"
                                    FontFamily="{TemplateBinding FontFamily}"
                                    FontSize="{Binding TitleFontSize, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:WindowEx}}}"
                                    Foreground="{Binding TitleForeground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:WindowEx}}}"
                                    Text="{TemplateBinding Title}" />
                                <!--  Border用于遮盖Title  -->
                                <Border
                                    Grid.Column=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值