WPF如何设置Button为直角
时间: 2024-04-15 18:29:04 浏览: 8
要将 WPF 中的 Button 设置为直角,您可以使用 Button 控件的样式和模板来自定义其外观。以下是一种常用的方法:
1. 在 XAML 文件中,定义一个新的样式并设置 TargetType 为 Button:
```xaml
<Window.Resources>
<Style x:Key="RoundedButtonStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="0">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
```
2. 在需要使用直角按钮的地方,将 Button 的 Style 属性设置为上述样式:
```xaml
<Button Content="Click me" Style="{StaticResource RoundedButtonStyle}" />
```
通过将 CornerRadius 属性设置为 0,我们可以使按钮的边角变为直角。您可以根据需要调整 CornerRadius 的值来实现不同的外观效果。
请注意,您可以根据自己的需求进行更多的样式和模板自定义。上述代码仅提供了一个基本示例来设置按钮为直角。