wpf 使用 C# 将文本框添加到网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25108179/
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
Add textbox to grid with C#
提问by SinTek Solutions
I am fairly new to C#. I am trying to add a textbox to the grid by clicking a button. When I click this button, everything on my grid disappears:
我对 C# 相当陌生。我正在尝试通过单击按钮向网格添加文本框。当我单击此按钮时,网格上的所有内容都会消失:
private void addLine_Click(object sender, System.EventArgs e)
{
System.Windows.Controls.TextBox txt = new System.Windows.Controls.TextBox();
txt.Name = "textBox8";
dxfLines.Children.Add(txt);
}
Here is the xaml too:
这里也是xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DXFGenerator" Height="350" Width="525" Loaded="Window_Loaded">
<Grid Name="dxfLines">
<TextBox Height="23" HorizontalAlignment="Left" Margin="271,94,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Button Content="Generate DXF" Height="23" HorizontalAlignment="Left" Margin="286,194,0,0" Name="button1" VerticalAlignment="Top" Width="84" Click="button1_Click" />
<Button Content="Add Line" Height="23" HorizontalAlignment="Left" Margin="391,196,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="addLine_Click" />
</Grid>
</Window>
回答by Reubene
If you are using a grid you need to define rows and columns. Your xaml should look something like this:
如果您使用网格,则需要定义行和列。您的 xaml 应如下所示:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DXFGenerator" Height="350" Width="525" Loaded="Window_Loaded">
<Grid Name="dxfLines" x:FieldModifier="private" >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Grid.Column="2" Grid.Row="4" />
<Button Content="Generate DXF" Height="23" HorizontalAlignment="Left" Grid.Column="1" Grid.Row="6" VerticalAlignment="Top" Width="84" Click="button1_Click" />
<Button Content="Add Line" Height="23" HorizontalAlignment="Left" Grid.Column="2" Grid.Row="6" VerticalAlignment="Top" Width="75" Click="addLine_Click" />
</Grid>
</Window>
You also need to specify a row and column for the text box you are adding so the grid knows where to place the text box. The content of your addLine click should look something like:
您还需要为要添加的文本框指定行和列,以便网格知道放置文本框的位置。您的 addLine 单击的内容应类似于:
System.Windows.Controls.TextBox txt = new System.Windows.Controls.TextBox();
txt.Name = "textBox8";
Grid.SetColumn(txt,1);
Grid.SetRow(txt, 1);
dxfLines.Children.Add(txt);
Besides, keep in mind that, in order to add a new child item, you select the Grid form and not the Window form . The Window form does not have the Childrenproperty, however in this example the Gridform has it.
此外,请记住,为了添加新的子项,您选择 Grid 表单而不是 Window 表单。Window 窗体没有Children属性,但是在本例中Grid窗体有它。

