Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

button inside border

I have a problem with a button inside a border. I want the button to fill the button space inside a border but the button is placed above the border and not under the border. And this way it hides the corder radius made by the border.

Here is a picture of my problem:

enter image description here

Someone knows how to put the button below the border?

Here is my xaml for my button:

<Button Name="filterCustomerBtn" 
        Command="{Binding Path=UpdateDepartments}" 
        Style="{StaticResource defaultButtonStyle}" 
        Width="200" 
        Margin="0, 15, 0, 0" 
        HorizontalAlignment="Center">Filter now</Button>

Here is the relevant xaml code:

<Window.Resources>
    <Style x:Key="defaultButtonStyle" TargetType="Button">
        <Setter Property="Margin" Value="2"></Setter>
    </Style>
</Window.Resources>


<StackPanel Orientation="Vertical" DockPanel.Dock="Top">
    <Border Style="{StaticResource MainBorderStyle}" Margin="2" Background="LightBlue">
        <StackPanel Orientation="Vertical" VerticalAlignment="Center">
            <Button Name="filterCustomerBtn" Command="{Binding Path=UpdateDepartments}" Style="{StaticResource defaultButtonStyle}" Width="200" Margin="0, 15, 0, 0" HorizontalAlignment="Center" Panel.ZIndex="-1">Filter now</Button>
        </StackPanel>
    </Border>
</StackPanel>

Result Image

enter image description here

like image 493
7heViking Avatar asked Sep 19 '26 00:09

7heViking


1 Answers

Since your border has a corner radius, there isn't much you can do in the context of the button's default styling to round its corners to make it look seamless with the border. I have run into this issue with a few different controls, including buttons. The solution to your problem is to create a ControlTemplate for the button. Within this template you will be able to set the radius of the bottom corners of your button, or all 4 corners if you wish, to the matching corner radius of your border. Click here for a good example of creating a rounded button template. Below I have attempted to simplify the blog code by removing the hover animation. Concentrate on the Border section as they are the main template.

<Style x:Key="RoundedButton" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="{TemplateBinding Background}"/>
    <Setter Property="Foreground" Value="{TemplateBinding Foreground}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="0,0,1,1"/>
    <Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">                        
            <Border 
                CornerRadius="5,5,5,5" 
                BorderThickness="1,1,1,1" 
                RenderTransformOrigin="0.5,0.5" 
                x:Name="border" 
                BorderBrush="#000000">
                    <Border.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="1" ScaleY="1"/>
                            <SkewTransform AngleX="0" AngleY="0"/>
                            <RotateTransform Angle="0"/>
                            <TranslateTransform X="0" Y="0"/>
                        </TransformGroup>
                    </Border.RenderTransform>
                    <Border 
                        Background="{TemplateBinding Background}" 
                        CornerRadius="5,5,5,5" 
                        x:Name="border1">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="0.5*"/>
                                <RowDefinition Height="0.5*"/>
                            </Grid.RowDefinitions>
                            <Border Grid.Row="0" CornerRadius="5,5,0,0">
                                <Border.Background>
                                    <LinearGradientBrush 
                                        EndPoint="0.5,1" 
                                        StartPoint="0.5,0">
                                        <GradientStop 
                                            Color="#00FFFFFF" 
                                            Offset="0"/>
                                        <GradientStop 
                                            Color="#7EFFFFFF" 
                                            Offset="1"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                            </Border>                                
                            <ContentPresenter 
                                VerticalAlignment="Center"  
                                Grid.RowSpan="2" 
                                HorizontalAlignment="Center" 
                                x:Name="contentPresenter"/>
                        </Grid>
                    </Border>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter 
                            Property="Opacity" 
                            TargetName="border1"  
                            Value="0.5"/>
                        <Setter 
                            Property="Opacity" 
                            TargetName="border" 
                            Value="1"/>
                        <Setter 
                            Property="Opacity" 
                            TargetName="contentPresenter" 
                            Value="0.5"/>
                    </Trigger>
                    <Trigger 
                        Property="IsPressed" 
                        Value="True">
                        <Setter 
                            Property="RenderTransform" 
                            TargetName="border">
                            <Setter.Value>
                                <TransformGroup>
                                    <ScaleTransform 
                                        ScaleX="0.9" 
                                        ScaleY="0.9"/>
                                    <SkewTransform 
                                        AngleX="0" 
                                        AngleY="0"/>
                                    <RotateTransform 
                                        Angle="0"/>
                                    <TranslateTransform 
                                        X="0" 
                                        Y="0"/>
                                </TransformGroup>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Also, here is a link to the MSDN page regarding this subject.

like image 78
Scott Boettger Avatar answered Sep 20 '26 14:09

Scott Boettger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!