Programmatically add label to grid

2019-03-19 23:05发布

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?

4条回答
家丑人穷心不美
2楼-- · 2019-03-19 23:32

In the shown code you never add the new grid to any window/control in a window.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-19 23:45

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
查看更多
ゆ 、 Hurt°
4楼-- · 2019-03-19 23:45

gride is the name of my gride in dynamicForm.xaml window. it creates button and add to gride, then in button click evnt it creates a label by clicking on a the button.

  public partial class DynamicForm : Window
    {

    Label lb = new Label();


  public DynamicForm()
    {
        InitializeComponent();

        Button dynamicButton = new Button();
        dynamicButton.Content = "Click me";
        Grid.SetRow(dynamicButton, 0);
        Grid.SetColumn(dynamicButton, 0);

        gride.Children.Add(dynamicButton);

        dynamicButton.Click += button1_Click;


    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {


       Label dynamicLabel = 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, 1);
       Grid.SetColumn(dynamicLabel, 0);

      gride.Children.Add(dynamicLabel);


    }

}
查看更多
放我归山
5楼-- · 2019-03-19 23:46

If you need to add a button or a Label (just change Button to Label) to a Grid, you can use this:

internal void FillbtnSubCat(Grid grid)
    {
        var myDefinition = new ColumnDefinition();

        var myButton = new Button();

        Grid.SetColumn(myButton, count);

        myButton.Margin = new Thickness(5, 10, 5, 25);
        myButton.MinWidth = 30;
        myButton.Content = count;

        myButton.Click+= new RoutedEventHandler(Click1);

        myDefinition.Width = new GridLength(68);

        grid.ColumnDefinitions.Add(myDefinition);

        grid.Children.Add(myButton);
        count++;
    }


void Click1(object sender, RoutedEventArgs e)
    {
        var myButton = sender as Button;
        MessageBox.Show(myButton.GetHashCode().ToString());
    }

and XAML

 <ListView Grid.Column="0" Margin="10,10,5,5" Grid.Row="1"  Grid.ColumnSpan="2">
        <Grid Name="Grid1" Height="100" Width="auto">

            </Grid>
    </ListView>
查看更多
登录 后发表回答