1

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;
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • This may be helpful http://stackoverflow.com/questions/2337421/given-uielementcollection-find-all-elements-that-have-stylea-and-change-them-t – kenny Jan 19 '13 at 00:13
  • `T` is a generic type enabling you to do things like `List` and `List`. `List` is capable of handling whatever valid type you send it. – ChiefTwoPencils Jan 19 '13 at 01:02
  • C.Lang- Thank you for the information about the 'T' type, that does make sense and definitely helps. – FishTechnology Jan 21 '13 at 15:36

1 Answers1

1

I am trying to iterate through my TextBoxes on a WPF form to clear the data

That is the wrong approach to begin with. In WPF, the Visual tree is a complex thing and navigating thru it can be complicated as well. Instead, you should change your mindset:

  • WPF is best suited to use the MVVM pattern, which encourages separation of application logic and data from presentation layer. I suggest you research on this pattern, otherwise you will find yourself hitting walls over and over trying to do "seemingly simple" things in WPF (such as clearing the TextBoxes).

  • You must learn to see the UI not as place to store data, but as a way to show data. Thus, the strings you actually want to clear are not properties of any UI elements, but instead they are part of either a Model or a ViewModel. I suggest you read up about MVVM and DataBinding in WPF.

  • Also, the fact that you named your TextBoxes TextBox3N1W09, TextBox3N1W10, etc. Makes me think you have to show a Collection of Data Items (that's the MVVM mindset coming into play), thus you'd better be doing this with an ItemsControl or one of its derivatives, and a proper DataTemplate. I suggest you read up on this concepts as well.

The bottom line is, WPF quickly discourages the manipulation of UI elements because of the complexity of the Visual Tree and other things such as Dispatcher thread affinity, etc. It may seem complicated at first glance, but you will quickly realize that applying the right pattern is going to simplify a whole lot your life.

here are some articles you can start reading:

http://en.wikipedia.org/wiki/Model_View_ViewModel

http://msdn.microsoft.com/en-us/library/ms750612.aspx

http://www.wpftutorial.net/

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • This is definitely some useful information, I have yet to delve into to any extent, but I do see the necessity in understanding this information better. If it were not for needing to learn on the job, I would love to spend a considerable amount of time on the matter. Thank you for your insight, there is no doubt this will help my future development. – FishTechnology Jan 21 '13 at 15:42