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

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

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,169 questions
No comments
{count} votes

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

    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);
    
    No comments

1 additional answer

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

    8758-tabitem.png

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

    No comments