0

This is my first time using WPF. It has been a bit of a nightmare, but I've done enough of the project that I now don't want to start all over again with a forms application.

My problem is this: I have 50 textboxes inside an expander, which in turn is inside a stackpanel. I need to store the value of each textbox in an array. In forms I would probably do it something like this (I am using vb.net by the way):

Dim i As Integer
Dim values() as string
For i = 0 To 49
values(i) = form1.Controls("TextBox" & i).text
Next i

(Assuming my textboxes were named Textbox1, Textbox2, Textbox3 etc.)

How can I do this in WPF? I've tried using Trees (I think they're called?) but have failed every time and now given up.

Oh, I also forgot to mention that there are also other controls in the expander (labels that are paired with the textboxes).

Arc
  • 329
  • 3
  • 11
  • 3
    Your approach is all wrong... in WPF values of UI elements are stored in properties and bound to UI elements using data binding. – Dean Kuga May 30 '12 at 21:32
  • You should be using data binding. What do these 50 strings represent? 50 different objects, or 50 different properties of a single object? – Trevor Elliott May 30 '12 at 21:45
  • The code I posted isn't something I am actually using, it was just an example intended to illustrate what I was looking for (unless I misunderstood what you said, in which case how do you know I am not using data binding?). The 50 strings I want to store in an array in the back end are the values from the textboxes, so I would guess they would count as 50 different objects, rather than 50 different properties. All I want to do is take each piece of text from each textbox in the expander and it in an array. Like I said this is my first time using WPF so I'm slightly confused. – Arc May 30 '12 at 21:59

1 Answers1

0

This goes to coding horror category but if you wish to pursue it anyway there is a method on FrameworkElement called FindName (http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.findname.aspx) which would do what you need. It searches it's child elements recursively, just name your expander and use FindName in code-behind to fetch all Textboxes.

dodsky
  • 504
  • 4
  • 11
  • It's ropy. I know this. Nevertheless I decided to continue pursuing this route, and I thank you for your useful, if upsetting, post. – Arc May 31 '12 at 00:10