Search code examples
wpfvb.nettextboxstackpanelexpander

WPF: Change value of all textboxes nested inside expanders in a stackpanel


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


Solution

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