17

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

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

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

4

4 回答 4

20

我通过命名另一个网格来纠正这个问题。我从来没有命名网格,因此这就是.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, 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
于 2013-09-11T17:38:12.273 回答
2

如果您需要将按钮或标签(只需将按钮更改为标签)添加到网格,您可以使用这个:

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

和 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>
于 2016-04-24T20:00:01.660 回答
0

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


    }

}
于 2014-04-29T07:37:09.167 回答
-1

在显示的代码中,您永远不会将新网格添加到窗口中的任何窗口/控件。

于 2013-09-06T13:51:48.840 回答