Search code examples
c#wpftabcontroltabitem

New TabItem at run time


Hi I am using TabControl to make Conference System, the private chats would be in a new TabItem. I am using this code to present a new member chatting in private:

TabItem ti = new TabItem();
ti.Header="Name";
MyTabControl.Items.Add(ti);

but the problem with this code is that I am adding a list box in the TabItem but the TabItem doesn't have a function to add an item in it. how can I add items into TabItem?

Second try: I used this code to present a new member chatting in private

ItemsControl it = new ItemsControl();
ListBox lst = new ListBox();
lst.Width = 571;
lst.Height = 301;

it.Items.Add(lst);
tabControlChat.Items.Add(it);

with this code I can add all items I need in the new tab. but the main problem is that I can't name the tab there is no property like (ti.Header) to name the tab. so what is the solution please? and thank you


Solution

  • Use this:

        TabItem ti = new TabItem();
        ti.Header = "Name";
        tabControl1.Items.Add(ti);
    
        ListBox lst = new ListBox();
        lst.Width = 571;
        lst.Height = 301;
    
        ti.Content = lst;