Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9138717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:17:36+00:00 2026-06-17T09:17:36+00:00

I’m trying to add a new TabItem to TabControl each time I click on

  • 0

I’m trying to add a new TabItem to TabControl each time I click on a button and I have no problem with that. But I want a textbox inside each TabItem. How do I do that? I need to do that with code I suppose.

TabItem newTab = new TabItem();
                newTab.Header = ncn.courseName;
                newTab.FontSize = 20;
                TextBox textbox = new TextBox();
                textbox.Width = 200;
                textbox.Height = 100;
                textbox.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                textbox.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                Grid grid = new Grid();
                grid.Children.Add(textbox);
                newTab.Content = grid;
                this.Courses.Items.Add(newTab);
                this.Courses.SelectedItem = newTab;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:17:36+00:00Added an answer on June 17, 2026 at 9:17 am

    If you would like to use only code and not the MVVM pattern, this can be solved this way:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        TabItem item = null;
        Grid grid = null;
        TextBox textbox = null;
    
        try
        {
            // Creating the TextBox
            textbox = new TextBox();
            textbox.Width = 200;
            textbox.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            textbox.VerticalAlignment = System.Windows.VerticalAlignment.Top;
    
            // Creating the Grid (create Canvas or StackPanel or other panel here)
            grid = new Grid();
            grid.Children.Add(textbox);     // Add more controls
    
            item = new TabItem();
            item.Header = "Hello, this is the new tab item!";
            item.Content = grid;            // OR : Add a UserControl containing all controls you like, OR use a ContentTemplate
    
            MyTabControl.Items.Add(item);
            MyTabControl.SelectedItem = item;   // Setting focus to the new TabItem
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error creating the TabItem content! " + ex.Message);
        }
        finally
        {
            textbox = null;
            grid = null;
            item = null;
        }
    }
    

    That is solving it “the old way” by using code-behind.

    If you on the other want to use the WPF like it should, you can do like this.
    To simplify a bit, I am using the code-behind as DataContext. I would recommend using a class instead in the running code.
    I have also used the Cutton click event instead if using the Button Command.

    First I create a “holder” class for the tab items, holding the data you need.

    TabItemHolder.cs

        public class TabItemHolder : DependencyObject, INotifyPropertyChanged
        {
            public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(String), typeof(TabItemHolder), new UIPropertyMetadata());
            public String Header
            {
                get { return (String)GetValue(HeaderProperty); }
                set
                {
                    SetValue(HeaderProperty, value);
                    NotifyPropertyChanged("Header");
                }
            }
    
            public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(TabItemHolder), new UIPropertyMetadata());
            public String Text
            {
                get { return (String)GetValue(TextProperty); }
                set
                {
                    SetValue(TextProperty, value);
                    NotifyPropertyChanged("Text");
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            public void NotifyPropertyChanged(String PropertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
    

    Then I have the model class, in this example the MainWindow.cs itself:

    MainWindow.cs

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
    public static readonly DependencyProperty SelectedTabProperty = DependencyProperty.Register(“SelectedTab”, typeof(TabItemHolder), typeof(MainWindow), new UIPropertyMetadata());
    public TabItemHolder SelectedTab
    {
    get { return (TabItemHolder)GetValue(SelectedTabProperty); }
    set
    {
    SetValue(SelectedTabProperty, value);
    NotifyPropertyChanged(“SelectedTab”);
    }
    }

    public static readonly DependencyProperty TabsProperty = DependencyProperty.Register("Tabs", typeof(ObservableCollection<TabItemHolder>), typeof(MainWindow), new UIPropertyMetadata());
    public ObservableCollection<TabItemHolder> Tabs
    {
        get { return (ObservableCollection<TabItemHolder>)GetValue(TabsProperty); }
        set
        {
            SetValue(TabsProperty, value);
            NotifyPropertyChanged("Tabs");
        }
    }
    
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        this.Tabs = new ObservableCollection<TabItemHolder>();
    }
    
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        this.Tabs.Add(new TabItemHolder() { Header = "Hello, this is the new tab item!", Text = "Dummy text for the textbox" });
        this.SelectedTab = this.Tabs[this.Tabs.Count - 1];
    }
    
    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(String PropertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
    }
    

    }

    And finally, the XAML would be something like this.

    MainWindow.xaml

        <Grid x:Name="LayoutRoot">
            <TabControl x:Name="MyTabControl"
                        Margin="12,67,12,12"
                        ItemsSource="{Binding Tabs}"
                        SelectedItem="{Binding SelectedTab}">
                <TabControl.ContentTemplate>
                    <DataTemplate>
                        <Grid>
                            <TextBox Text="{Binding Path=Text}"
                                     Width="200"
                                     HorizontalAlignment="Left"
                                     VerticalAlignment="Top" />
                        </Grid>
                    </DataTemplate>
                </TabControl.ContentTemplate>
                <TabControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Header}"/>
                    </DataTemplate>
                </TabControl.ItemTemplate>
            </TabControl>
            <Button Content="Button" Height="34" HorizontalAlignment="Left" Margin="19,12,0,0" Name="button1" VerticalAlignment="Top" Width="90" Click="button1_Click" />
        </Grid>
    

    That would do the same trick in a different (and in my opinion better) way.

    I hope that helps you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.