Alex Rivera | Logout

WPF C# Programmatically adding and moving tabs

Asked 2011-02-11T11:16:23.713
11

I'm currently working on something that is probably done in plenty of examples out there. But after some searching I can't find anything.

I'm working with WPF tab control and I'm trying to recreate some basic functionality (which you see in all internet browsers nowadays) to add a new tab by clicking a '+' tab which is the last listed tab.

I already have the '+' tab which adds a new tab. My problem is, I want to move the '+' tab after the new tab (so its the end tab again) and switch view to the new tab that has just been created.

I thought something like:

    void tiNewTab_Add(object sender, EventArgs e)
    {
        int idx = tabControl1.Items.Count;
        tabControl1.SelectedIndex = idx - 1;
        TabItem ti = new TabItemKPI();
        tabControl1.Items.Add(ti);
        tabControl1.Items.MoveCurrentToLast();
    }

...would work but no luck :(

Edit
Report

1 Answer

0

Not tested, but following should work:

int idx = tabControl1.Items.Count;
tabControl1.SelectedIndex = idx - 1;
TabItem ti = new TabItem();
tabControl1.Items.Insert(tabControl1.Items.IndexOf(tabControl1.Items.Last()), ti);
answered 2011-02-11T11:26:50.293

Your Answer