Search code examples
c#wpfmvvmwpf-controlstabcontrol

How to get all children and their values from a Window containing Tabcontrol


EDIT

Straightforward question

I have been trying for the past 2 days I could find a way to fetch all the children along with the data from a TabControl which has multiple TabItems in it. it looks like this

enter image description here

The TABULAR TabControl has three TabItems (TEMPERATURE, TEMPERATURE1, AND PRESSURE) each TabItem contains textboxes and sliders. I want to loop through TabItems fetching all the textbox values and slider values.

I have tried the following methods but I was only able to see the data in properties when I add the variables to the watch window in debug mode.

METHODS I'VE TRIED

int tabular_items = TabularJsonEditor.Items.Count;
                sock.sendData(tabular_items.ToString());
                foreach(var rh in TabularJsonEditor.Items.SourceCollection)
                {
                    string h = rh.ToString();
                    var coll = TabularJsonEditor.SelectedContent;
                    
                }
                var iEnumeratorOftoBeIterated = TabularJsonEditor.Items.SourceCollection.GetEnumerator();
                while (iEnumeratorOftoBeIterated.MoveNext())
                {
                    Console.WriteLine($"The current value is: {iEnumeratorOftoBeIterated.Current}");
                    var curr = iEnumeratorOftoBeIterated.Current;
                    //var res = LogicalTreeHelper.GetChildren(curr);
                    var rest = ListBindingHelper.GetList(curr);
                    var temp = GetVisualChild(0);
                    
                }

WATCH WINDOW

enter image description here


Solution

  • use VisualTreeHelper to get child controls, and test type of child

    static public void EnumVisual(Visual myVisual)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
        {
            // Retrieve child visual at specified index value.
            Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);
    
            if (childVisual is Slider slider)
                Debug.Print(slider.Value);
    
            // Enumerate children of the child visual object.
            EnumVisual(childVisual);
        }
    }