简体   繁体   中英

c# xaml creating a button when another button is clicked

I haven't worked with xaml before and then I wondered. I need to do adding new buttons to stacklayout when another button is clicked. I know there should be an event handler when the button is clicked:

void OnAddNewButtonClicked(object sender, EventArgs e)
{
     . . .

}

but I don't know what to write in it at all. At the same time, I need the buttons to be created exactly in a certain stacklayout on the application page. Please tell me, I will be very grateful for your help.

First, you should name the stackLayout. Then, you can create buttons and add them to that stacklayout from code behind.

Here is a basic implementation as a wpf application.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <Button
        Grid.Column="0"
        Width="150"
        Height="30"
        Content="Add New Button"
        Click="Button_Click"/>

    <StackPanel
        x:Name="ButtonsPanel"
        Grid.Column="1"
        Background="Aqua" />
</Grid>

In code behind, you can create a new button and add to the "ButtonsPanel".

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Button btn = new Button();

        btn.Content="New Button";

        ButtonsPanel.Children.Add(btn);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM