Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add label to grid

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?

like image 824
StealthRT Avatar asked Sep 06 '13 13:09

StealthRT


People also ask

How do you add a label to a grid?

Select the column grid you want to label. Click Column Grid tab Label panel Label. Specify the grid location for labels: If you want to…

How do I create a dynamic grid in WPF?

The Grid class in WPF represents a Grid control. The following code snippet creates a Grid control, sets its width, horizontal alignment, vertical alignment, show grid lines, and background color. Grid DynamicGrid = new Grid();

How do I add a grid in WPF?

First Method: By typing XAML CodeRowDefinitions property and a ColumnDefinition Element for each column inside the Grid. ColumnDefinitions property. By default, GridLines are invisible. For showing the GridLines, set the ShowGridLines property of the Grid to True.


2 Answers

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
like image 148
StealthRT Avatar answered Oct 22 '22 21:10

StealthRT


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>
like image 22
koly86 Avatar answered Oct 22 '22 21:10

koly86