WPF标题栏自定义
作者:互联网
1、废话不多说直接上代码
<Window.Resources>
<Style x:Key="btnTitleMaxMin" TargetType="Button">
<Setter Property="Width" Value="30"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="Cursor" Value="Hand"></Setter>
<Setter Property="Margin" Value="1,0,1,0"></Setter>
<Setter Property="Foreground" Value="DarkRed"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="columnDefinition1" Width="0.982*"/>
<ColumnDefinition x:Name="columnDefinition" Width="0.018*"/>
</Grid.ColumnDefinitions>
<Border x:Name="rectangle" Background="Transparent" BorderThickness="0" CornerRadius="0" Grid.ColumnSpan="2"></Border>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="rectangle" Value="RoyalBlue"></Setter>
<Setter Property="Foreground" Value="white"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Transparent"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="btnTitleClose" TargetType="Button">
<Setter Property="Width" Value="35"></Setter>
<Setter Property="Height" Value="35"></Setter>
<Setter Property="Cursor" Value="Hand"></Setter>
<Setter Property="Margin" Value="1,0,1,0"></Setter>
<Setter Property="Foreground" Value="DarkRed"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="columnDefinition1" Width="0.982*"/>
<ColumnDefinition x:Name="columnDefinition" Width="0.018*"/>
</Grid.ColumnDefinitions>
<Border x:Name="rectangle" Background="Transparent" CornerRadius="0,4,0,0" Grid.ColumnSpan="2"></Border>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="rectangle" Value="Firebrick"></Setter>
<Setter Property="Foreground" Value="white"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Transparent"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="CustomWindowTemplate" TargetType="Window">
<Border BorderBrush="#FF4949" CornerRadius="4">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
</Grid.RowDefinitions>
<Border Background="#FF4949" BorderBrush="Transparent" MouseDown="Grid_MouseDown" CornerRadius="1,1,0,0">
<Grid Name="Title" Margin="0">
<TextBlock HorizontalAlignment="Left" Margin="10,0,0,0" Text="{TemplateBinding Title}" VerticalAlignment="Center"></TextBlock>
<Button ToolTip="最小化" FontSize="20" Content="0" FontFamily="Webdings" Name="btnMin" Click="btnMin_Click" HorizontalAlignment="Right" Foreground="White" Margin="0,-13,42,0" Height="26" Width="26" Style="{DynamicResource btnTitleMaxMin}" VerticalAlignment="Center" />
<Button ToolTip="最大化" FontSize="20" Content="1" FontFamily="Webdings" Name="btnMaxOrMin" Click="btnMaxOrMin_Click" HorizontalAlignment="Right" Foreground="White" Margin="0,-9,21,0" Height="26" Width="26" Style="{DynamicResource btnTitleMaxMin}" VerticalAlignment="Center" />
<Button ToolTip="关闭" FontSize="20" Content="r" FontFamily="Webdings" Name="PART_CLOSEBTN" HorizontalAlignment="Right" Foreground="White" Margin="0,-9,1,0" Height="26" Width="26" Style="{DynamicResource btnTitleClose}" VerticalAlignment="Center" Click="Button_Click" />
</Grid>
</Border>
<Grid Grid.Row="1" Background="White" >
<Grid Margin="5,5,5,0">
<AdornerDecorator>
<ContentPresenter></ContentPresenter>
</AdornerDecorator>
</Grid>
</Grid>
<Grid Grid.Row="2" Background="White">
<ResizeGrip Name="WindowResizeGrip" Visibility="Collapsed" IsTabStop="False" HorizontalAlignment="Right" FontWeight="Black" VerticalAlignment="Bottom">
</ResizeGrip>
</Grid>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="ResizeMode" Value="CanResizeWithGrip">
<Setter TargetName="WindowResizeGrip" Property="Visibility" Value="Visible"></Setter>
</Trigger>
<Trigger Property="WindowState" Value="Maximized">
<Setter TargetName="btnMaxOrMin" Property="Content" Value="2"></Setter>
<Setter TargetName="btnMaxOrMin" Property="ToolTip" Value="还原"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
最后在绑定Template="{DynamicResource CustomWindowTemplate}"
标签:自定义,标题栏,DynamicResource,Template,WPF,CustomWindowTemplate 来源: https://www.cnblogs.com/lisghxfdfgh/p/16646966.html