wpf 以编程方式将标签添加到网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18659435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically add label to grid
提问by StealthRT
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?
但是,按下按钮后,我再也看不到网格上的任何东西……我错过了什么?
回答by StealthRT
I corrected this by naming the other grid. I never had the grid named and therefore thats why the .Children.Addnever 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 insidethat it was needing for this part.
我通过命名另一个网格来纠正这个问题。我从来没有命名过网格,因此这就是为什么.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
回答by koly86
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
和 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>
回答by H.B.
In the shown code you never add the new grid to any window/control in a window.
在显示的代码中,您永远不会将新网格添加到窗口中的任何窗口/控件。
回答by nizam uddin
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.
gride 是我在 dynamicForm.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);
}
}