How do you make a fully square button?

14,979

Solution 1

If you mean to have width = height then see WPF dynamic layout: how to enforce square proportions (width equals height)?

If you mean to have square corners then set the CornerRadius of the Border to zero:

<ControlTemplate x:Key="SquareButton" TargetType="{x:Type Button}">  
<Border CornerRadius="0"/>  
</ControlTemplate>  

Then the button uses that template:

<Button Template="{StaticResource SquareButton}"/>

Solution 2

Just create a custom button style...something like this

 <Style x:Key="ButtonStyle" TargetType="Button">
    <Setter Property="FontFamily"
            Value="Arial Narrow" />
    <Setter Property="FontSize"
            Value="13" />
    <Setter Property="Width" Value="50"/>
    <Setter Property="Height" Value="20"/>
    <Setter Property="Margin" Value="3"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="BorderBrush" Value="#FF1733D2"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="VerticalAlignment" Value="Top"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="Border" Background="#FF1733D2">
                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

If you have Blend click on the button... edit style -> edit current... and get rid of the corner radius I think

Share:
14,979
Phu Minh Pham
Author by

Phu Minh Pham

Updated on June 17, 2022

Comments

  • Phu Minh Pham
    Phu Minh Pham almost 2 years

    Is there a simple way to make a fully square Button? Normally, a Button is a little rounded, so how do I achieve this?

  • Ivan Crojach Karačić
    Ivan Crojach Karačić about 12 years
    He wants to get rid of the rounding as far as I can see
  • Phu Minh Pham
    Phu Minh Pham about 12 years
    That is correct, but still maintaining the button effect. Ivan Crojach Karačić I have used your solution but I also want to keep the button effect. Do I really have to use Blend to do that or can you do it all i XAML?
  • kaj
    kaj about 12 years
    You just need to update the control template for your button which is why I'm only showing the relevant bit. This is just a case of editing XAML
  • Ivan Crojach Karačić
    Ivan Crojach Karačić about 12 years
    You don't need to have Blend but it's a nice to have if you want to make something that needs to look a little bit better :)