c# - 使用 C# 将文本框添加到网格

标签 c# wpf textbox grid

我是 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);
    }

这也是 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>

最佳答案

如果您使用网格,则需要定义行和列。您的 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>

您还需要为要添加的文本框指定行和列,以便网格知道放置文本框的位置。 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);

此外,请记住,为了添加新的子项,您选择的是 Grid 窗体而不是 Window 窗体。 Window 窗体没有 Children 属性,但是在本例中 Grid 窗体有它。

关于c# - 使用 C# 将文本框添加到网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25108179/

相关文章:

c# - "IN"Linq 运算符

具有接口(interface)的 C# MVC 通用存储库类

c# - “T”不包含定义

c# - 如何使用 Asp.Net Core 2.2 在子域上服务器图像?

c# - WPF 组合框 : Different template in textbox and drop-downlist

wpf - 将(非多)ValueConverter 应用为 MultiBinding + StringFormat 的输出

mysql - 使用文本框输入将用户名和密码插入到连接字符串VB

c# - 在我需要基于 XAML 控件进行计算的情况下,使用 MVVM 与控件交互的正确设计是什么?

c++ - 从另一个应用程序窗口中的文本字段读取

javascript - 使用循环创建Word Scrambler