在网格中动态创建文本框

发布于 2025-02-04 00:54:59 字数 977 浏览 2 评论 0原文

这可能是骗子,但我找不到可以帮助我提供解决方案的信息。

我基本上有一个stackpanel,它的网格带有定义的行和列,我想在不同的行和列中动态创建一定的文本框,然后我可以用来与之交互。

到现在为止,我的解决方案是在WPF中逐一创建每个文本框,然后将所有这些文本框一个一个一个一个一个一个一个将它们添加到C#中的文本框列表中以与它们进行交互。但是我真的不相信这是一个干净的解决方案。

示例WPF

<TextBox x:Name="Std2018_01" Grid.Row="1" Grid.Column="1" Height="20" Width="50" TextAlignment="Center" BorderBrush="{x:Null}" TextChanged="Std2018_TextChanged"></TextBox>
<TextBox x:Name="Std2018_02" Grid.Row="2" Grid.Column="1" Height="20" Width="50" TextAlignment="Center" BorderBrush="{x:Null}" TextChanged="Std2018_TextChanged"></TextBox>
<TextBox x:Name="Std2018_03" Grid.Row="3" Grid.Column="1" Height="20" Width="50" TextAlignment="Center" BorderBrush="{x:Null}" TextChanged="Std2018_TextChanged"></TextBox>

C#将它们添加到列表中,

        boxes2018.Add(Std2018_01);
        boxes2018.Add(Std2018_02);
        boxes2018.Add(Std2018_03);

必须有一种更好的方法来解决此问题。

This probably is a dupe, but I wasn't able to find the information that would help me produce a solution.

I basically have a Stackpanel, which has a Grid with defined Rows and Columns and I want to dynamically create a set amount of textboxes in differing Rows and Columns, that I can then use to interact with.

My solution up until now was to create every textbox one by one in WPF and then add all of them one by one to a List of Textboxes in C# to interact with them. But I really don't believe that this is a clean solution.

Example WPF

<TextBox x:Name="Std2018_01" Grid.Row="1" Grid.Column="1" Height="20" Width="50" TextAlignment="Center" BorderBrush="{x:Null}" TextChanged="Std2018_TextChanged"></TextBox>
<TextBox x:Name="Std2018_02" Grid.Row="2" Grid.Column="1" Height="20" Width="50" TextAlignment="Center" BorderBrush="{x:Null}" TextChanged="Std2018_TextChanged"></TextBox>
<TextBox x:Name="Std2018_03" Grid.Row="3" Grid.Column="1" Height="20" Width="50" TextAlignment="Center" BorderBrush="{x:Null}" TextChanged="Std2018_TextChanged"></TextBox>

C# Adding them to a List

        boxes2018.Add(Std2018_01);
        boxes2018.Add(Std2018_02);
        boxes2018.Add(Std2018_03);

There has to be a better way to solve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文