wpf 动态设置TextBox文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14078330/
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
Dynamically set TextBox text
提问by GArchitech
I have multiple XAML TextBoxes, each of which manipulate a corresponding value in an array, when the value in the TextBoxis changed, using a C#method which dynamically checks which TextBoxhas called the method.
我有多个 XAML TextBoxes,每个XAML es 操作数组中的相应值,当 中的值TextBox更改时,使用C#动态检查TextBox调用该方法的方法。
<TextBox x:Name="_0_0" TextChanged="_x_y_TextChanged"/>
<TextBox x:Name="_0_1" TextChanged="_x_y_TextChanged"/>
<TextBox x:Name="_0_2" TextChanged="_x_y_TextChanged"/>
// And so on.....
each of which manipulate a corresponding value in an array, when the value in the TextBoxis changed, using a C# method which dynamically checks which TextBoxhas called the method.
每个都操作数组中的相应值,当 中的值TextBox更改时,使用动态检查TextBox调用该方法的 C#方法。
private void _x_y_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox current = (TextBox)sender;
string currentname = current.Name;
string rowstring = currentname.Substring(1, 1);
string columnstring = currentname.Substring(3, 1);
int row = Convert.ToInt32(rowstring);
int column = Convert.ToInt32(columnstring);
// I've then detected the name of the textbox which has called it...
So this information can be used to dynamically store information from a TextBoxin a corresponding array index - or whatever you want to do with it...
因此,此信息可用于TextBox在相应的数组索引中动态存储来自 a 的信息- 或者您想用它做什么...
My question however, is:
然而,我的问题是:
How can I create a method which uses index locations in my array, to call the relevant TextBoxand update its text?
如何创建使用数组中索引位置的方法来调用相关内容TextBox并更新其文本?
回答by jam40jeff
Use FindName(string)to find the text box by name as follows (where containeris a control that contains all of the text boxes):
用于FindName(string)按名称查找文本框,如下所示(其中container是包含所有文本框的控件):
private void UpdateTextBox(int row, int column, string text)
{
TextBox textBox = container.FindName("_" + row + "_" + column) as TextBox;
if(textbox != null)
{
textbox.Text = text;
}
}
回答by theodox
There are two ways you might go:
你可能有两种方式:
If you have a lot of data to manage, or if you can't predict the length of the array, it would be better to bind to a collection instead of manually poking data into and out of an array. If you create a class derived from ObservableCollection instead of using an array the data <> ui relationship is pretty trivial.
如果您有大量数据需要管理,或者您无法预测数组的长度,那么最好绑定到一个集合,而不是手动将数据放入和取出数组。如果您创建一个从 ObservableCollection 派生的类而不是使用数组,则 data <> ui 关系非常简单。
if you really need to do this manually, maybe it would be better to stick the index into the 'tag' field of your text boxes. You could (a) see it clearly in your xaml, (b) parse it easily and (c) if you used a variation on the formula here:
如果您真的需要手动执行此操作,也许最好将索引粘贴到文本框的“标签”字段中。您可以 (a) 在您的 xaml 中清楚地看到它,(b) 轻松解析它,以及 (c) 如果您在这里使用了公式的变体:
Find all controls in WPF Window by type
you could iterate over the textboxes in window and find the right one by looking at its tag index:
您可以遍历窗口中的文本框并通过查看其标签索引找到正确的文本框:
foreach (TextBox t in FindVisualChildren<TextBox>(this))
{
if ((int) t.Tag) == my_index )
{
t.Text = "my_text_goes_here";
}
}
回答by Mats Magnem
I would go in the direction of the answer I gave on this question: form anchor/dockIn short, I would create a class that holds the actual values and then create a collection that holds information classes.
我会按照我在这个问题上给出的答案的方向前进: form anchor/dock简而言之,我将创建一个包含实际值的类,然后创建一个包含信息类的集合。
Then I would not use the event "TextChanged" on the TextBoxes, rather "sniff" for changes on the Dependency Property used to hold the text. This can easily be done in the Dependency Property.
然后我不会在文本框上使用事件“TextChanged”,而是“嗅探”用于保存文本的依赖属性的更改。这可以在依赖属性中轻松完成。
Last, I would use an ItemsControl or ItemsPresenter to show the controls. Number of controls will follow number of items in the collection.
最后,我将使用 ItemsControl 或 ItemsPresenter 来显示控件。控件的数量将遵循集合中的项目数量。
回答by M.A.K
I suggest using MVVM pattern, data template, and ItemsControl for handling this problem effectively.
我建议使用 MVVM 模式、数据模板和 ItemsControl 来有效地处理这个问题。

