Add stack panel when check box is checked

353 Views Asked by At

I want to add stack panel dynamically when check box is checked and if check box is unchecked then i want to remove that stack panel. I tried following code. when i run the program and check the check box nothing is happening.

private void bCheckbox_Checked(object sender, RoutedEventArgs e)
    {
        var stackpanel = new StackPanel{ Orientation = Orientation.Vertical };
        stackpanel.SetValue(Grid.RowProperty, 2);
        stackpanel.SetValue(Grid.ColumnProperty, 1);

        var textblockstart = new TextBlock { Text = "loop start time", FontSize = 18, 
                                            Margin = new Thickness(20,20,0,0)};
        var textboxstart = new TextBox { Name = "StartTextBox", Height=27, Width=110, 
                                         Margin = new Thickness(20,10,0,0) };
        var textblockend = new TextBlock { Text = "loop end time", FontSize = 18, 
                                            Margin = new Thickness(20,20,0,0)};
        var textboxend = new TextBox { Name = "EndTextBox", Height=27, Width=110,
                                         Margin = new Thickness(20,10,0,0) };

        stackpanel.Children.Add(textblockstart);
        stackpanel.Children.Add(textboxstart);
        stackpanel.Children.Add(textblockend);
        stackpanel.Children.Add(textboxend);

    }
1

There are 1 best solutions below

1
On BEST ANSWER

What you can do is create the StackPanel in the xaml markup itself with the Visibility set to Collapsed and just toggle the visibility of the StackPanel on the check event of CheckBox