WPF ControlTemplate 设置圆形按钮半径等于其宽度的一半
WPF ControlTemplate set rounded button radius equal to half of its width
我想使用 ControlTemplate 创建一个圆形按钮。这是我的代码,当我的宽度和高度等于 30 时,它可以完美地工作。我可以知道,是否可以将 RadiusX 和 RadiusY 设置为等于按钮 Width
的一半(例如 RadiusX={Width/2})?
<ControlTemplate x:Key="RoundButton" TargetType="Button">
<Grid>
<Rectangle RadiusX="15" RadiusY="15"
Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
Fill="{TemplateBinding Background}" />
<ContentPresenter Margin="3"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="RoundButton" TargetType="Button">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid x:Name="PART_Grid" Visibility="Hidden"/>
<Rectangle Grid.ColumnSpan="2"
RadiusX="{Binding ActualWidth, ElementName=PART_Grid, Mode=OneWay}"
RadiusY="{Binding ActualWidth, ElementName=PART_Grid, Mode=OneWay}"
Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
Fill="{TemplateBinding Background}" />
<ContentPresenter Grid.ColumnSpan="2" Margin="3"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
- - 恕我直言,这是多余的。
最好删除。
我想使用 ControlTemplate 创建一个圆形按钮。这是我的代码,当我的宽度和高度等于 30 时,它可以完美地工作。我可以知道,是否可以将 RadiusX 和 RadiusY 设置为等于按钮 Width
的一半(例如 RadiusX={Width/2})?
<ControlTemplate x:Key="RoundButton" TargetType="Button">
<Grid>
<Rectangle RadiusX="15" RadiusY="15"
Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
Fill="{TemplateBinding Background}" />
<ContentPresenter Margin="3"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="RoundButton" TargetType="Button">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid x:Name="PART_Grid" Visibility="Hidden"/>
<Rectangle Grid.ColumnSpan="2"
RadiusX="{Binding ActualWidth, ElementName=PART_Grid, Mode=OneWay}"
RadiusY="{Binding ActualWidth, ElementName=PART_Grid, Mode=OneWay}"
Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
Fill="{TemplateBinding Background}" />
<ContentPresenter Grid.ColumnSpan="2" Margin="3"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
- - 恕我直言,这是多余的。
最好删除。