Add new TabItem to a TabControl using Click Event [closed]

Posted on

Problem

I’m trying to add a new TabItem to a TabControl. The TabItem’s content will be set to a new Frame, and the frame holds the actual Page. A new Tabitem is added each time the button is clicked, this is the code I’ve come up with.

        // initiates the tab item ang assign dumby values (REMOVE BEFORE RELEASE)
        TabItem NewSupportTabItem = new TabItem { Header = "Support #123-98A", ToolTip = "New support ticket #123-98A",
         Name = "NewSupportTabItem"};

        // Creates the Frame
        Frame NewSupportFrame = new Frame();

        // Initializes the main Ticket Page
        SupportTicketDataShell TicketShell = new SupportTicketDataShell();

        // Set the content of the Frame to the Page
        NewSupportFrame.Content = TicketShell;

        // Sets the content of the TabItem to the Frame
        NewSupportTabItem.Content = NewSupportFrame;

        // Adds the TabItem to the TabControl now
        MainTabControl.Items.Add(NewSupportTabItem);

        // Focuses the tab
        NewSupportTabItem.Focus();

As with everything in programming, there’s an efficient way, a good way and a bad way, and I think I’m leaning more on the latter side.

Solution

I would suggest learning about Custom Controls. You can take an existing control add whatever properties, etc. you want to the control and every time you initialize a new one, all your defaults will be present. Here’s a reasonably good tutorial for Custom Controls

Leave a Reply

Your email address will not be published. Required fields are marked *