按程序添加和移动选项卡

时间:2022-09-10 15:51:45

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.

我正在使用WPF选项卡控件,我正在尝试重新创建一些基本功能(现在在所有的internet浏览器中都可以看到),通过单击“+”选项卡添加一个新选项卡,这是最后一个列出的选项卡。

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.

我已经有了“+”选项卡,它添加了一个新的选项卡。我的问题是,我想要在新选项卡之后移动“+”选项卡(因此它是end选项卡),并将视图切换到刚刚创建的新选项卡。

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

…会有用但不会带来好运

Any ideas?

什么好主意吗?

Thanks in advance.

提前谢谢。

2 个解决方案

#1


7  

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)

这样做是因为您总是至少有一个选项(+ 1)

Then select the second last one by

然后选择第二个

tabControl1.SelectedIndex=tabControl1.Items.Count-2;

#2


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

#1


7  

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)

这样做是因为您总是至少有一个选项(+ 1)

Then select the second last one by

然后选择第二个

tabControl1.SelectedIndex=tabControl1.Items.Count-2;

#2


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