我已经动态创建了一个面板。如何在WPF中动态创建的面板上动态创建按钮?
下面是我的代码:
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
Dim MyButtonUC As buttons_UC
Dim MyPanelUC As panels_UC
Dim catname As String = InputBox("Please enter category name here")
Dim MyButtonUC = New Button
MyButtonUC.Name = "btn" & catname.Replace(" ", "_").ToString
MyButtonUC.Content = catname
MyButtonUC.FontSize = 15
MyButtonUC.Height = 40
MyButtonUC.Width = 250
MyButtonUC.Margin = New Thickness(0, 3, 0, 0)
pnlCategoryButtonCont.Children.Add(MyButtonUC)
Dim MyPanelUC As New WrapPanel
MyPanelUC.Background = Brushes.Blue
MyPanelUC.Name = "pnl" & catname.Replace(" ", "_").ToString
pnlCategoryPnlContainer.Children.Add(MyPanelUC)
End Sub
在上面的示例中,我在任何面板上创建了一个按钮,还在另一个面板上创建了一个面板(A)。现在,我想在面板(A)上动态添加一些按钮。由于panel(A)是动态创建的,因此我无法直接调用它,或者我必须使用字符串来调用它,以便可以将Button作为子元素添加在其上。