在WPF,button的默认形态时长方形的,但是我们为了美观,通常需要将button的四个角设计的温和一点,在WPF中就需要用到这样一个属性CornerRadius。
首先我们要画一个button
<Button Click="SubmitButton" Style="{StaticResource NormalButton}" Content="确认" Background="#037AA6" Foreground="#FAFBFD" />
然后给button添加样式
<Style x:Key="NormalButton" TargetType="{x:Type Button}">
<Setter Property="FontFamily" Value="GEInspiraSans-Bold" />
<Setter Property="FontSize" Value="14" />
<Setter Property="Height" Value="32"/>
<Setter Property="Width" Value="96"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="3" BorderBrush="#037AA6" BorderThickness="1" Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
可以看到为border设置了CornerRadius属性,value为3,这个可以根据需求自定义圆角的弯曲度,同时上面将Background通过绑定的方式输入value,那么在画button的时候可以将值传入