选中复选框时添加堆栈面板

Add stack panel when check box is checked

我想在选中复选框时动态添加堆栈面板,如果未选中复选框,则我想删除该堆栈面板。我尝试了以下代码。 当我 运行 程序并选中复选框时,没有任何反应。

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);

    }

您可以做的是在 xaml 标记本身中创建 StackPanel,并将 Visibility 设置为 Collapsed,然后切换 [=10] 的可见性=] CheckBox

的检查事件