I am very new to C# and I am lost once again.I hate to ask this question, but I am beyond lost in understanding how this works. I am trying to iterate through my TextBoxes on a WPF form to clear the data.
All of my textboxes have the prefix "TextBox" which precedes a string of characters 6 characters long, ie (TextBox3N1W09, TextBox3N1W10...). I think I want to use the VisualTreeHelper, but I am not smart enough to:
#1) Pass a variable in. I think I understand the parent,
XAML < Grid Name="ThisGrid" >
in my case I named the Grid:"ThisGrid" but I am not understanding how to pass in the TextBoxes, I do not know if I need to do something like an iteration through the names of the TextBoxes and pass them as strings or if I can simply have the VisualTreeHelper look at all my the TextBoxes within the Grid.
#2) Once these textbox names or textbox objects are passed in, I am not sure what to do with the result:
{
return child as T;
}
I completely lost on what "T" is, what do I do with the "T"? Do I need to do something in the method or something where I called the method from. Like I said at the beginning, I just want to clear the textboxes, this does not seem that difficult, but I am having a heck of a time with it.
public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
string controlName = child.GetValue(Control.NameProperty) as string;
if (controlName == name)
{
return child as T;
}
else
{
T result = FindVisualChildByName<T>(child, name);
if (result != null)
return result;
}
}
return null;
}