简体   繁体   English

WPF在自定义标题栏中制作Windows 10关闭按钮

[英]WPF making Windows 10 close button in custom title bar

I need to build a custom title bar for my WPF application with only the close button, which currently looks like this:我需要为我的 WPF 应用程序构建一个自定义标题栏,只有关闭按钮,目前看起来像这样:

在此处输入图像描述

This is the code:这是代码:

<Grid Width="32" Height="25" Margin="1" HorizontalAlignment="Right" MouseLeftButtonUp="OnCloseWindow" Grid.Column="4">
    <Rectangle Fill="#FFE81123" Height="25" VerticalAlignment="Bottom"/>
    <TextBlock Text="╳" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#DDFFFFFF" />
</Grid>

What I wanted is a replica of the default Windows 10 close button animation, the button gradually turning red on hover and white again when you stop hovering.我想要的是默认 Windows 10 关闭按钮动画的复制品,该按钮在悬停时逐渐变为红色,当您停止悬停时再次变为白色。

How can I accomplish this?我怎样才能做到这一点?

You can achieve it by adding您可以通过添加来实现它

WindowStyle="ToolWindow"

in

        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" WindowStyle="ToolWindow">

It will show the close button only.它只会显示关闭按钮。

If you want the same style as the normal window, You need to create the style of the close button.如果您想要与普通窗口相同的样式,您需要创建关闭按钮的样式。

Check this link for reference检查此链接以供参考

https://www.codemag.com/article/1903031/Create-a-Title-Bar-for-User-Controls https://www.codemag.com/article/1903031/Create-a-Title-Bar-for-User-Controls

This is what I use...这是我用的...

Style:风格:

<Style TargetType="{x:Type Button}" x:Key="WindowControlRed">
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="Opacity" Value="1"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}" >
                    <Border Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="0">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="IndianRed"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="0.25"/>
            </Trigger>
        </Style.Triggers>
    </Style>

Button in XAML: XAML 中的按钮:

<Button Content="&#xE106;" FontFamily="Segoe MDL2 Assets" Height="40" FontSize="12" Width="40" Style="{StaticResource WindowControlRed}" Click="Close_Click"/>

Code behind:后面的代码:

private void Close_Click(object sender, RoutedEventArgs e)
{
    Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM