Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set CornerRadius on button template

I want to have a Button that defines no CornerRadius and two others that do, how can I achieve this?

<Style TargetType="Button" x:Key="TabButton">     <Setter Property="Background" Value="White" />     <Setter Property="TextBlock.TextAlignment" Value="Center" />     <Setter Property="Template">         <Setter.Value>             <ControlTemplate TargetType="Button">                 <Border CornerRadius="0" Background="White" BorderBrush="#ccc" BorderThickness="0,1,1,0" >                     <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>                 </Border>             </ControlTemplate>         </Setter.Value>     </Setter> </Style>  <Style TargetType="Button" x:Key="TabButtonFirst" BasedOn="{StaticResource TabButton}">     <Setter Property="CornerRadius" Value="3,0,0,0" /> </Style>  <Style TargetType="Button" x:Key="TabButtonLast" BasedOn="{StaticResource TabButton}">     <Setter Property="CornerRadius" Value="0,0,0,3" /> </Style> 
like image 389
Chris Avatar asked Jul 16 '13 15:07

Chris


People also ask

How do I make rounded corners on a button?

You can make rounded corners button by adding border-radius of 5px to 10px on HTML buttons.

How do you make a button corner rounded in WPF?

You can achieve rounded corners on a button without having to write any XAML (other than a Style, once) and without having to replace the template or set/change any other properties. Just use an EventSetter in your style for the button's "Loaded" event and change it in code-behind.

How do you make a button border round in CSS?

To make the div's borders rounded, you could add the following styling: border-radius: 15px; The above sets a 15 pixel radius on the top-left, top-right, bottom-left and bottom-right corners of the element. The higher the value of the radius, the more rounded the edge becomes.


1 Answers

You're not limited to the dependency properties of the control you're templating. In this case, while Button does not have a CornerRadius property, Border does, so you can use Border.CornerRadius instead:

<Style TargetType="Button" x:Key="TabButton">     <Setter Property="Background" Value="White" />     <Setter Property="TextBlock.TextAlignment" Value="Center" />     <Setter Property="Template">         <Setter.Value>             <ControlTemplate TargetType="Button">                 <Border CornerRadius="{TemplateBinding Border.CornerRadius}"                          Background="White" BorderBrush="#ccc"                          BorderThickness="0,1,1,0" >                     <ContentPresenter x:Name="contentPresenter"                                        ContentTemplate="{TemplateBinding ContentTemplate}"                                        Content="{TemplateBinding Content}"                                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"                                       Margin="{TemplateBinding Padding}"                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>                 </Border>             </ControlTemplate>         </Setter.Value>     </Setter> </Style>  <Style TargetType="Button" x:Key="TabButtonFirst" BasedOn="{StaticResource TabButton}">     <Setter Property="Border.CornerRadius" Value="3,0,0,0" /> </Style>  <Style TargetType="Button" x:Key="TabButtonLast" BasedOn="{StaticResource TabButton}">     <Setter Property="Border.CornerRadius" Value="0,0,0,3" /> </Style> 

With this approach, you no longer need to maintain multiple copies of your control template.

like image 67
Pieter Witvoet Avatar answered Sep 19 '22 08:09

Pieter Witvoet