提问者:小点点

WPF ControlTemplate设置舍入按钮半径等于其宽度的一半


我想使用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>

共1个答案

匿名用户

    <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}”--这是多余的。最好移除。