'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.
Solution 1:[1]
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);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | karachai |
