嘿所有我想使用以下代码向网格添加标签:
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
但是,按下按钮后,我从未在网格上看到任何内容......我缺少什么?
答案 0 :(得分:13)
我通过命名其他网格来纠正这个问题。我从来没有命名网格,因此这就是 .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
答案 1 :(得分: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>
答案 2 :(得分:0)
在显示的代码中,您永远不会将新网格添加到窗口中的任何窗口/控件。
答案 3 :(得分:0)
gride是动态Form.xaml窗口中我的网格的名称。它创建按钮并添加到gride,然后在按钮单击事件中,它通过单击按钮创建标签。
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);
}
}