Home > Back-end >  How to create a TextBox every time the button is pressed
How to create a TextBox every time the button is pressed

Time:03-08

The project I'm currently working on consists of Xamarin, and whenever I press a button on the screen, I need to add a specific UI.

Whenever a button is pressed, is there a way to add a specific UI (even to the Grid property)?

The code below is the ui xaml to be added.

<StackLayout x:Name="ScheduleDateTimeLayout" Style="{StaticResource FieldStackLayout}" IsVisible="{Binding IsAdvancedMode}">
                <Grid x:Name="ScheduleDateTimeGrid" Margin="0,0,0,10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="2*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <StackLayout Grid.Column="0" VerticalOptions="Center">
                        <Label Text="{localization:Translate FieldScheduleDateTime}"/>
                        <TimePicker x:Name="ScheduleTimePicker"
                            VerticalOptions="CenterAndExpand"
                            Time="{Binding AlarmTime}"
                            Grid.Column="1"/>
                        <Label Text="{localization:Translate FieldScheduleCycle}"/>
                        <Picker x:Name="ScheduleCyclePicker" SelectedItem="{Binding ScheduleCycle}"/>
                    </StackLayout>

                    <StackLayout Grid.Column="1"
                             Margin="10,0,0,0"
                             Spacing="{DynamicResource TopFieldSpacing}"
                             VerticalOptions="Center">
                        <Button x:Name="DeleteTimeButton"
                                Text="Delete Time"
                                HorizontalOptions="FillAndExpand"
                                Margin="0,20,0,0"
                                BackgroundColor="Red"
                                TextColor="White"/>
                    </StackLayout>
                </Grid>

CodePudding user response:

Do you want to add some Views(e.g. Label) to a Grid when clicking a Button?

If yes, you can achieve this using C# in code-behind.

Please refer to the following code:

    private void Button_Clicked(object sender, EventArgs e)
    {

        //First add two rows
        grdDataGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
        grdDataGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });

        //First add some data columns to the grid
        //grdDataGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
        //grdDataGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });


        //Add data to each row
        grdDataGrid.Children.Add(new Label() { Text = "Row 1 Col 0" }, 0, 1);
        grdDataGrid.Children.Add(new Label() { Text = "Row 1 Col 1" }, 1, 1);

        grdDataGrid.Children.Add(new Label() { Text = "Row 2 Col 0" }, 0, 2);
        grdDataGrid.Children.Add(new Label() { Text = "Row 2 Col 1" }, 1, 2);
    }

Note:

1.grdDataGrid is the x:Name of a Grid. You can also add some other UI (e.g. StackLayout with some controls) to the Grid.

2.Since you have define two ColumnDefinition in xaml, so I commented out the ColumnDefinitions code in above code , you can modify above code according to your requirement.

  • Related