WPF C# Programmatically adding and moving tabs

核能气质少年 提交于 2019-11-30 03:38:48

问题


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


回答1:


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;



回答2:


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);


来源:https://stackoverflow.com/questions/4968367/wpf-c-sharp-programmatically-adding-and-moving-tabs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!