如何从字符串 C# WPF 动态调用文本框?

发布于 2024-10-18 20:42:30 字数 541 浏览 2 评论 0原文

所以我有 70 个“节点”,它们都是 WPF 中的文本框,我试图通过函数调用更改文本框中的值。

我有一个函数,称为:

private void changeNode(int row, int column, int cost)  
{    
       int nodeNumber= row * 10 + column;     
       call node"nodeNumber".Text = Convert.String(cost); 
       //example node0.Text = Convert.String(cost); 
}

我确定要更改的节点,然后调用 nodeX.Text 来更改它,但是我希望 X 成为一个变量,而不是必须创建 70 个案例,在其中调用适当的文本框。

我看到了几种通过反射执行此操作的方法,但它似乎只有在函数没有参数并且位于函数内而不是 XAML 中的文本框时才有效。

让我知道是否有一种简单的方法可以将字符串“node37”转换为调用 node37.Text = cost 或类似的方法。

So I have 70 "nodes" which are all textboxes in WPF and I'm trying to change the value in the textbox from a function call.

I have a function called:

private void changeNode(int row, int column, int cost)  
{    
       int nodeNumber= row * 10 + column;     
       call node"nodeNumber".Text = Convert.String(cost); 
       //example node0.Text = Convert.String(cost); 
}

I determine what node I want to change then call nodeX.Text to change it however I want X to be a variable that I can rather than having to create 70 cases where I call the appropriate textbox.

I saw a couple of ways of doing this with reflection however it seemed to only work if the function had no parameters and also was within the function not a textbox in XAML.

Let me know if there is a simple way to convert say a string "node37" to call node37.Text = cost or something like that.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枕梦 2024-10-25 20:42:30

听起来你的方法是错误的。为什么有一组代表文本框名称的字符串?相反,您应该在内存中引用 TextBox 对象。如果您有多个,并且不知道有多少个,则可以使用 TextBox 对象的 array 来代替。您可以使用代表您要与之交互的文本框的数字对数组进行索引。

避免使用反射,这里完全没有必要。

Sounds like your approach is wrong. Why do you have a set of strings which represent the names of the textboxes? You should instead have in-memory references to TextBox objects. If you have more than one, and you don't know how many there will be, then use an array of TextBox objects instead. You can index into the array with the number that represents the textbox you're looking to interact with.

Avoid the use of reflection, it is completely unnecessary here.

旧竹 2024-10-25 20:42:30

我假设您已经为所有文本框输入了名称(如果没有,您可以动态执行此操作)。然后,您可以使用此问题的答案按名称查找适当的控件。

I assume you have put names for all your textboxes (you can do this dynamically if you haven't). Then you can use the answers for this question to find the appropriate control by name.

A君 2024-10-25 20:42:30

您的所有文本框都是同一画布或其他控件的子控件吗?循环遍历子项并将控件添加到字典中。解析名称以获取编号并将其用作密钥。

Are all your textboxes children of the same canvas or other control? Loop through the children and add the controls to a dictionary. Parse the name to get the number and use that as the key.

怂人 2024-10-25 20:42:30

当你处理数据时,使用 List 总是更好。使用要加载的 DataObject 创建 ObservableCollection,现在处理数据对象而不是实际的控件。

在 WPF 中,如果遵循规则,则不应指向实际对象。在此处检查示例应用程序:
http://www.abhisheksur.com/2010/08/woring -with-icollectionviewsource-in.html

我想你会明白的。

It is always better to use List when you are dealing with Data. Create an ObservableCollection with the DataObjects which you want to load, and now deal with the Data object rather than actual Controls.

In WPF, if you follow the rules, you should not point to the actual object. Check the sample application here :
http://www.abhisheksur.com/2010/08/woring-with-icollectionviewsource-in.html

I think you will get the approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文