嘿所有我想使用以下代码向我的网格添加标签:
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click Dim dynamicLabel As New Label() Dim g As New Grid dynamicLabel.Name = "NewLabel" dynamicLabel.Content = "TEST" dynamicLabel.Width = 240 dynamicLabel.Height = 30 dynamicLabel.Margin = New Thickness(0,21,0) dynamicLabel.Foreground = New SolidColorBrush(Colors.White) dynamicLabel.Background = New SolidColorBrush(Colors.Black) Grid.SetRow(dynamicLabel,0) Grid.SetColumn(dynamicLabel,6) g.Children.Add(dynamicLabel) End Sub
但是,按下按钮后,我从未在网格上看到任何东西…我缺少什么?
我通过命名其他网格来纠正这个问题.我从来没有命名网格,因此这就是.Children.Add永远不会添加到网格的原因.我试过mainGrid.Children.Add(mainGrid是我的父网格的名称),它总是会抛出一个错误,因为它是这个部分需要的网格内部.
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.Windows.RoutedEventArgs) //Handles Button1.Click Dim dynamicLabel As new Label(); dynamicLabel.Name = "NewLabel"; dynamicLabel.Content = "TEST"; dynamicLabel.Width = 240; dynamicLabel.Height = 30; dynamicLabel.Margin = new Thickness(0,0); dynamicLabel.Foreground = new SolidColorBrush(Colors.White); dynamicLabel.Background = new SolidColorBrush(Colors.Black); Grid.SetRow(dynamicLabel,0); Grid.SetColumn(dynamicLabel,0); childGrid.Children.Add(dynamicLabel); //'<-changed to grid name in XAML properties End Sub