Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through TabItems in a TabControl

I have a tabcontrol with 9 tabitems in it. Each tab has as series of TextBoxes for the user to enter data. At the bottom is a clear button, hooked up to this method:

public void ClearTextBoxes()
    {
        ChildControls ccChildren = new ChildControls();

        foreach (object o in ccChildren.GetChildren(rvraDockPanel, 2))
        {
            if (o.GetType() == typeof(WatermarkTextBox) || o.GetType() == typeof(DateBox) ||
                o.GetType() == typeof(DigitBox) || o.GetType() == typeof(PhoneBox))
            {
                WatermarkTextBox water = (WatermarkTextBox)o;
                water.Text = "";
            }
            else if (o.GetType() == typeof(ComboBox))
            {
                ComboBox combo = (ComboBox)o;
                combo.SelectedIndex = -1;
            }
            else if (o.GetType() == typeof(CheckBox))
            {
                CheckBox check = (CheckBox)o;
                check.IsChecked = false;
            }
        }
    }

This works perfectly fine, however I also have a MenuItem that allows the user to ClearAll tabs. Right now the clear button only clears what's on the currently selected tab, and leaves everything else alone. My thought on how to do this was to iterate through the tabitems with this loop:

        for (int i = 0; i < 10; i++ )
        {
            tabSelection.SelectedIndex = i;
            clearButton_Click(null, null);
        }

It will flip through all the tabs, but won't clear anything. I have tried using Automation instead, with the same result. It just won't seem to clear anything.

ChildControls class:

class ChildControls
{
private List<object> listChildren;

public List<object> GetChildren(Visual p_vParent, int p_nLevel)
{
    if (p_vParent == null)
    {
        throw new ArgumentNullException("Element {0} is null!", p_vParent.ToString());
    }

    this.listChildren = new List<object>();

    this.GetChildControls(p_vParent, p_nLevel);

    return this.listChildren;

}

private void GetChildControls(Visual p_vParent, int p_nLevel)
{
    int nChildCount = VisualTreeHelper.GetChildrenCount(p_vParent);

    for (int i = 0; i <= nChildCount - 1; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(p_vParent, i);

        listChildren.Add((object)v);

        if (VisualTreeHelper.GetChildrenCount(v) > 0)
        {
            GetChildControls(v, p_nLevel + 1);
        }
    }
}

}

like image 478
Keven M Avatar asked Feb 19 '26 05:02

Keven M


1 Answers

WPF discards the entire VisualTree of a TabItem which isn't selected, so no controls actually exist on tabs that are not visible.

To maintain control values (such as Selections, Text, etc), they need to be bound to something. If they're bound to something then your Clear() method should actually clear the Bound values, not the UI values. If they're not bound to anything, then the TabControl will revert to its initial state when selected.

like image 110
Rachel Avatar answered Feb 20 '26 18:02

Rachel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!