Home > Back-end >  How to set the CornerRadius of a button programmatically [WPF/VB]?
How to set the CornerRadius of a button programmatically [WPF/VB]?

Time:07-13

I know how to set the corner radius of a button in XAML.

However, I need to set the corner radius of buttons that are created programmatically (dynamic buttons), during runtime. Here's a code example:

    Dim pt As Thickness

    Dim mySolidColorBrush As SolidColorBrush = New SolidColorBrush()
    mySolidColorBrush = CType((New BrushConverter().ConvertFrom("#005EB8")), SolidColorBrush)

    pt.Top = 95
    
    If howMany > 0 Then 
        For i = 0 To howMany - 1
            Buttons(i) = New Button With {
                .Tag = i,
                .Margin = New Thickness(5, 5, 5, 0),
                .Width = 120,
                .Height = 60,
                .Foreground = New SolidColorBrush(Colors.White),
                .Background = mySolidColorBrush,
                .VerticalAlignment = VerticalAlignment.Top,
                .HorizontalAlignment = HorizontalAlignment.Left,
                .FontSize = 16,
                .Content = btnName(i)
                }
            AddHandler Buttons(i).Click, AddressOf ButtonClickDyna
            PNL_Main.Children.Add(Buttons(i))
            pt.Top  = 45
        Next
    End If

I was not able to find such information on the internet and have been going through the properties of a button one by one, sadly with no success.

Any help will be much appreciated as I am still a newbie.

CodePudding user response:

Similar to the linked XAML based solution, I suggest you utilize styles when you create your dynamic buttons. Suppose you create some named resource styles in your window

€dit: in my first version I defined a separate named style for the Border, but as @ASh mentioned, the Border style can be nested within the Button style.

<Window.Resources>
    <Style x:Key="MyButtonStyle" TargetType="Button">
        <Style.Ressources>
            <Style TargetType="Border">
                <Setter Property="CornerRadius" Value="5"/>
            </Style>
        </Style.Ressources>
        <Setter Property="Margin" Value="5 5 5 0"/>
        <Setter Property="Width" Value="120"/>
        <Setter Property="Height" Value="60"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Background" Value="#005EB8"/>
        <Setter Property="VerticalAlignment" Value="Top"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="FontSize" Value="16"/>
    </Style>
</Window.Resources>

Then you can assign the MyButtonStyle to each created button.

The following code is in C#, but you should be able to create the equivalent VB code

private void Initializer_OnClick(object sender, RoutedEventArgs e)
{
    var buttonStyle = (Style) FindResource("MyButtonStyle");

    for (int i = 1; i <= 2; i  )
    {
        ButtonsContainer.Children.Add(new Button
        {
            Content = $"Button {i}",
            Style = buttonStyle
        });
    }
}

Usage example, where the "Initializer" Button dynamically creates two more buttons:

<Grid>
    <StackPanel Name="ButtonsContainer" Orientation="Horizontal"
                VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
    <Button Content="Initializer"
            Style="{StaticResource MyButtonStyle}"
            VerticalAlignment="Center" HorizontalAlignment="Center"
            Click="ButtonBase_OnClick">
        <Button.Resources>
            <Style TargetType="Border" BasedOn="{StaticResource MyButtonBorderStyle}"></Style>
        </Button.Resources>
    </Button>
</Grid>

CodePudding user response:

feel free to fix any syntax errors

Dim border = DirectCast(Buttons(i).Template.FindName("border", Buttons(i)), Border);
border.CornerRadius = 5;
  • Related