Dynamically defined text TextBox

I have several XAML TextBox es, each of which controls the corresponding value in the array when the value in the TextBox changes using the C# method, which dynamically checks which TextBox called the method.

  <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 controls the corresponding value in the array when the value in the TextBox changed using the C # method, which dynamically checks which TextBox called the method.

  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... 

Thus, this information can be used to dynamically store information from a TextBox in the corresponding index of the array - or what you want to do with it ...

My question, however, is this:

How to create a method that uses the location of indexes in my array, call the corresponding TextBox and update its text?

+4
source share
4 answers

Use FindName(string) to find the text field by name as follows (where container is the control that contains all the text fields):

 private void UpdateTextBox(int row, int column, string text) { TextBox textBox = container.FindName("_" + row + "_" + column) as TextBox; if(textbox != null) { textbox.Text = text; } } 
+6
source

You can go in two ways:

If you have a lot of data to manage or if you cannot predict the length of the array, it would be better to bind to the collection instead of manually outputting data to and from the array. If you create a class derived from an ObservableCollection, instead of using an array, the data relation <> ui is pretty trivial.

if you really need to do this manually, it might be better to insert an index into the tag field of your text fields. You could (a) clearly see this in your xaml, (b) easily parse it, and (c) if you used a variant of the formula:

Find all controls in a WPF window by type

you can iterate over text fields in a window and find the correct one by looking at the index of your tag:

  foreach (TextBox t in FindVisualChildren<TextBox>(this)) { if ((int) t.Tag) == my_index ) { t.Text = "my_text_goes_here"; } } 
+2
source

I suggest using the MVVM pattern, data pattern, and ItemsControl to efficiently handle this problem.

+1
source

I would go in the direction of the answer I gave on this question: anchor / dock In short, I would create a class that contains the actual values, and then create a collection that stores information classes.

Then I would not use the TextChanged event in TextBoxes, but rather "sniffed" for changes in the dependency property used to store text. This is easy to do in Dependency.

Finally, I would use ItemsControl or ItemsPresenter to show the controls. The number of controls will correspond to the number of elements in the collection.

+1
source

All Articles