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
Author by
Phu Minh Pham
Updated on June 17, 2022Comments
-
Phu Minh Pham almost 2 years
Is there a simple way to make a fully square
Button
? Normally, aButton
is a little rounded, so how do I achieve this? -
Ivan Crojach Karačić about 12 yearsHe wants to get rid of the rounding as far as I can see
-
Phu Minh Pham about 12 yearsThat 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 about 12 yearsYou 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ć about 12 yearsYou 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 :)