Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF C# Programmatically adding and moving tabs

Tags:

c#

wpf

tabcontrol

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 :(

like image 650
SumGuy Avatar asked Feb 11 '11 11:02

SumGuy


People also ask

Is WPF and C# same?

C# is a programming language. WPF is a technology used to develop rich GUI applications using C# (or any other . NET language).

Is WPF still relevant 2021?

WPF is still one of the most used app frameworks in use on Windows (right behind WinForms).

What is WPF in C# used for?

Windows Presentation Foundation (WPF) is a UI framework that creates desktop client applications. The WPF development platform supports a broad set of application development features, including an application model, resources, controls, graphics, layout, data binding, documents, and security.

What replaced WPF?

Universal Windows Platform. Both Windows Forms and WPF are old, and Microsoft is pointing developers towards its Universal Windows Platform (UWP) instead. UWP is an evolution of the new application platform introduced in Windows 8 in 2012.


1 Answers

Try something like this:

tabControl1.Items.Insert(tabControl1.Items.Count-1,ti); 

This will do because you always have at least one TabItem (the + one)

Then select the second last one by

tabControl1.SelectedIndex=tabControl1.Items.Count-2;
like image 57
HCL Avatar answered Nov 04 '22 22:11

HCL