Programmatically add label to grid

故事扮演 提交于 2020-01-01 02:26:08

问题


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?


回答1:


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



回答2:


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>



回答3:


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




回答4:


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);


    }

}


来源:https://stackoverflow.com/questions/18659435/programmatically-add-label-to-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!