wpf – 以编程方式将标签添加到网格

前端之家收集整理的这篇文章主要介绍了wpf – 以编程方式将标签添加到网格前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
嘿所有我想使用以下代码向我的网格添加标签
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
原文链接:https://www.f2er.com/vb/255532.html

猜你在找的VB相关文章