如何从字符串 C# WPF 动态调用文本框?
所以我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来你的方法是错误的。为什么有一组代表文本框名称的字符串?相反,您应该在内存中引用
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 anarray
ofTextBox
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.
我假设您已经为所有文本框输入了名称(如果没有,您可以动态执行此操作)。然后,您可以使用此问题的答案按名称查找适当的控件。
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.
您的所有文本框都是同一画布或其他控件的子控件吗?循环遍历子项并将控件添加到字典中。解析名称以获取编号并将其用作密钥。
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.
当你处理数据时,使用 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.