How to append TabItem to TabControl in C# instead of XAML

Nicholas Piazza 526 Reputation points
2020-05-29T23:24:48.527+00:00

I'm experimenting with WPF applications. Using the designer I created a TabControl with two TabItems that are set up in XAML. Then I wanted to see about creating a third TabItem in C# code and attaching it to the TabControl using code instead of XAML. Here is what I have so far.

However, at this point, I can't find any method or property in TabControl that lets me append the new TabItem. Is there a way to do this in C# code?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,663 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points
    2020-06-01T02:11:28.917+00:00

    Welcome to our Microsoft Q&A platform!
    You can set your TabControl name as 'MyTabControl', then add the 'MyTabControl.Items.Add(Converters); ' in C# code.
    Step1:

    <TabControl Name="MyTabControl">
    

    Step2:

    TabItem Converters = new TabItem();
                Converters.Header = "Unit converters";
                Converters.Background = Brushes.Transparent;
                MyTabControl.Items.Add(Converters);
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Nicholas Piazza 526 Reputation points
    2020-05-29T23:28:02.093+00:00

    8758-tabitem.png

    Forgot to attach this image in previous post. Didn't see a way to edit the post.

    0 comments No comments