Search code examples
c#wpftabcontrolvisualtreehelper

C# WPF VisualTree for pageframe


Working with WPF, C# and a tabbed page window. I'm trying to cycle through all controls on the form, but its failing to do so with the VisualTreeHelper.

If I have a window with a TabControl on it, and three TabItems on that tab control, visually, it all works fine...

HOWEVER, if I try to use the visual tree helper, it only exposes content based on whichever SINGLE page is active, not all possible pages.

Any ideas on how to confirm whats on ALL pages, even though only one can be "visible" at a time?

I hope its something very simple I'm missing...


Solution

  • If you look through the template of TabControl, you will find that the content of TabControl will only be the selected TabItem:

    Content="{TemplateBinding TabControl.SelectedContent}"
    

    This is done to optimize the performance of TabControl. Since the controls other in tabItems are not in view hence could not be found in Visual tree. However, they do exist in the Logical Tree. This link might get you started - Exploring WPF logical and visual tree

    Or as a workaround refer to this, to persist the Visual tree while switching tabs in TabControl - Persist Visual Tree