圆形按钮有很多种实现方式,例如第一种方式在样式中用Ellipse画一个圆形,宽度和高度一致。第二种方式用Border设置Width,Height,CornerRadius一致也可以画圆。第三种方式用Path用矢量图画圆。
一、以下第一种方式在样式中用Ellipse画一个圆形实现方式
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource {x:Static SystemColors.ActiveCaptionBrushKey}}"/>
<Setter Property="BorderBrush" Value="#3099C5"/>
<Setter Property="IsHitTestVisible" Value="True"/>
<Setter Property="Width" Value="64"/>
<Setter Property="Height" Value="64"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Grid SnapsToDevicePixels="True">
<Ellipse Width="{TemplateBinding Width}" Height="{TemplateBinding Width}"
Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}" />
<ContentControl Content="{TemplateBinding Content}" Focusable="False"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
二、以下第二种方式用Border设置Width,Height,CornerRadius一致画圆
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource {x:Static SystemColors.ActiveCaptionBrushKey}}"/>
<Setter Property="BorderBrush" Value="#3099C5"/>
<Setter Property="IsHitTestVisible" Value="True"/>
<Setter Property="Width" Value="64"/>
<Setter Property="Height" Value="64"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Border Background="{TemplateBinding Background}" Width="{TemplateBinding Width}"
CornerRadius="{Binding ActualHeight, RelativeSource={RelativeSource Self}}" Height="{TemplateBinding Height}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
//此处修改CornerRadius="5" 可转换为圆角矩形
<ContentControl Content="{TemplateBinding Content}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
三、第三种方式用Path用矢量图画圆
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource {x:Static SystemColors.ActiveCaptionBrushKey}}"/>
<Setter Property="BorderBrush" Value="#3099C5"/>
<Setter Property="IsHitTestVisible" Value="True"/>
<Setter Property="Width" Value="64"/>
<Setter Property="Height" Value="64"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Grid>
<Viewbox Stretch="Uniform">
<Grid>
<Path Fill="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}" Data="M510.537016 1014.078844c-67.964659 0-133.907178-13.315997-195.994961-39.576884-59.959897-25.361049-113.805489-61.663802-160.04137-107.897688-46.234883-46.23588-82.536638-100.081472-107.897687-160.041369C20.342111 644.474122 7.026114 578.532601 7.026114 510.567942s13.315997-133.907178 39.576884-195.994962c25.361049-59.959897 61.663802-113.805489 107.897687-160.041369 46.23588-46.234883 100.080475-82.536638 160.04137-107.897687C376.629838 20.373037 442.572357 7.05704 510.537016 7.05704S644.444194 20.373037 706.531977 46.633924c59.959897 25.361049 113.805489 61.663802 160.04137 107.897687 46.234883 46.23588 82.536638 100.080475 107.897687 160.041369 26.260887 62.087783 39.576884 128.030302 39.576884 195.994962s-13.315997 133.907178-39.576884 195.994961c-25.361049 59.959897-61.663802 113.805489-107.897687 160.041369-46.23588 46.234883-100.080475 82.536638-160.04137 107.897688-62.088781 26.260887-128.030302 39.576884-195.994961 39.576884z m0-967.117707c-255.633631 0-463.606804 207.973174-463.606804 463.606805s207.973174 463.606804 463.606804 463.606804 463.606804-207.973174 463.606804-463.606804S766.170647 46.961137 510.537016 46.961137z"/>
<Path Fill="{TemplateBinding Background}" Data="M512 512m-512 0a512 512 0 1 0 1024 0 512 512 0 1 0-1024 0Z"/>
</Grid>
</Viewbox>
<ContentControl Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
标签:自定义,方式,C#,样式,圆形,UI,画圆,按钮,CornerRadius From: https://www.cnblogs.com/guangzhiruijie/p/17118513.html