Lorenzo Ang
Lorenzo Ang

Reputation: 1318

How to get a UserControl inside a TabItem?

XAML Code:

<TabControl TabStripPlacement="Left" Margin="-3,-3,-3,-3" Background="Transparent" SelectionChanged="TabControl_SelectionChanged">
        <TabItem Header="Corn">
            <Grid x:Name="sdfsfd"/>
        </TabItem>
        <TabItem Header="TabItem" >
            <local:CornTab x:Name="cornTab"/>
        </TabItem>
    </TabControl>

CS Code:

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) {
        TabItem ti = ((TabControl)sender).SelectedItem as TabItem;
        Console.WriteLine(ti.Header);

        getChildrenOf(ti, 1);
    }

    private void getChildrenOf(DependencyObject dep, int indents) {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dep); i++) {
            var child = VisualTreeHelper.GetChild(dep, i);
            Console.WriteLine($"{new String('\t', indents)}{i}: {child.GetType()}");

            getChildrenOf(child, indents + 1);
        }
    }

I'm trying to retrieve <local:CornTab x:Name="cornTab"/> when I click the TabItem but no matter what I do, it won't seem to come out in VisualTreeHelper and the printouts are the same with the only difference being the TabItem.Header.

TabItem
0: System.Windows.Controls.Border
    0: System.Windows.Controls.Grid
        0: MahApps.Metro.Controls.ContentControlEx
            0: System.Windows.Controls.Grid
                0: System.Windows.Controls.ContentPresenter
                    0: System.Windows.Controls.TextBlock
        1: MahApps.Metro.Controls.Underline
            0: System.Windows.Controls.Border
                0: MahApps.Metro.Controls.ClipBorder
                    0: System.Windows.Controls.ContentPresenter
Corn
    0: System.Windows.Controls.Border
        0: System.Windows.Controls.Grid
            0: MahApps.Metro.Controls.ContentControlEx
                0: System.Windows.Controls.Grid
                    0: System.Windows.Controls.ContentPresenter
                        0: System.Windows.Controls.TextBlock
            1: MahApps.Metro.Controls.Underline
                0: System.Windows.Controls.Border
                    0: MahApps.Metro.Controls.ClipBorder
                        0: System.Windows.Controls.ContentPresenter

Perhaps I need to traverse the control tree differently?

Upvotes: 0

Views: 172

Answers (1)

nosale
nosale

Reputation: 818

You don't need to traverse the visual tree.

TabItem is a ContentControl and ContentControl sets the ContentPropertyAttribute as "Content"

System.Windows.Markup.ContentPropertyAttribute:

Indicates which property of a type is the XAML content property. A XAML processor uses this information when processing XAML child elements of XAML representations of the attributed type.

Therefore you can simply change your method to this

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{
    TabItem ti = ((TabControl)sender).SelectedItem as TabItem;
    var content = ti.Content;
    if(content is CornTab cornTab)
    {
        //Do with 'cornTab' whatever you want
    }
}

Upvotes: 1

Related Questions