以编程方式将标签添加到网格 [英] Programmatically add label to grid

查看:43
本文介绍了以编程方式将标签添加到网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试使用以下代码向我的网格添加标签:

Hey all i am trying to add a label to my grid using the following code:

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, 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

但是,按下按钮后,我再也看不到网格上的任何东西......我错过了什么?

However, i never see anything on the grid after i push the button... what am i missing?

推荐答案

我通过命名另一个网格更正了这个问题.我从来没有给网格命名,因此这就是 .Children.Add 永远不会添加到网格的原因.我尝试了 ma​​inGrid.Children.Add(mainGrid 是我父网格的名称)它总是会抛出一个错误,因为它需要的是 inside 的网格部分.

I corrected this by naming the other grid. I never had the grid named and therefore thats why the .Children.Add never would add to the grid. I tried mainGrid.Children.Add (mainGrid is the name of my parent grid) and it would always throw an error because it was the grid inside that it was needing for this part.

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, 21, 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

这篇关于以编程方式将标签添加到网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆