在Title后直接写TextBlock居中即可。
<Window ... Title="Demo" TextBlock.TextAlignment="Center" ...>
public partial class WelcomeWindow : Window{
public WelcomeWindow(){
// 设置窗体居中
WindowStartupLocation = WindowStartupLocation.CenterScreen;
InitializeComponent();
}
}
只保留最小化和关闭按钮
<Window ... ResizeMode="CanMinimize" ...>
先写资源,其中Border的CornerRadius控制了按钮的弧度
<Window.Resources>
<ControlTemplate x:Key="roundCornerBtnTemplate" TargetType="Button">
<Border BorderThickness="1" BorderBrush="DarkGray" CornerRadius="5" Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
Border>
ControlTemplate>
Window.Resources>
使用时:
<Button ...Template="{StaticResource roundCornerBtnTemplate}".../>
举例:写一个按钮样式的菜单,点击菜单,出现下拉列表
但是因为按钮本身可以点击,会禁掉菜单的效果,即无法出现下拉列表。此时,只要给button加个属性:IsHitTestVisible=“False”
<Menu Background="Transparent" Height="42" Width="150" HorizontalAlignment="Left">
<MenuItem HorizontalContentAlignment="Stretch">
<MenuItem.Header>
<Button x:Name="helpBtn" ... IsHitTestVisible="False">
...
Button>
MenuItem.Header>
<MenuItem Click="helpBtn_Click">
<MenuItem.Header>
<TextBlock Text="用户手册" HorizontalAlignment="Center"/>
MenuItem.Header>
MenuItem>
<Separator/>
<MenuItem>
<MenuItem.Header>
<TextBlock Text="设置参数" HorizontalAlignment="Center"/>
MenuItem.Header>
MenuItem>
MenuItem>
Menu>