[英]WPF making Windows 10 close button in custom title bar
我需要為我的 WPF 應用程序構建一個自定義標題欄,只有關閉按鈕,目前看起來像這樣:
這是代碼:
<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>
我想要的是默認 Windows 10 關閉按鈕動畫的復制品,該按鈕在懸停時逐漸變為紅色,當您停止懸停時再次變為白色。
我怎樣才能做到這一點?
您可以通過添加來實現它
WindowStyle="ToolWindow"
在
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">
它只會顯示關閉按鈕。
如果您想要與普通窗口相同的樣式,您需要創建關閉按鈕的樣式。
檢查此鏈接以供參考
https://www.codemag.com/article/1903031/Create-a-Title-Bar-for-User-Controls
這是我用的...
風格:
<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>
XAML 中的按鈕:
<Button Content="" FontFamily="Segoe MDL2 Assets" Height="40" FontSize="12" Width="40" Style="{StaticResource WindowControlRed}" Click="Close_Click"/>
后面的代碼:
private void Close_Click(object sender, RoutedEventArgs e)
{
Close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.