WPF C#以编程方式添加和移动选项卡 [英] WPF C# Programmatically adding and moving tabs
问题描述
我目前正在做一些可能在许多示例中完成的事情.但是经过一番搜索,我什么都找不到.
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.
我正在使用WPF选项卡控件,并且尝试通过单击"+"选项卡(这是最后列出的选项卡)来重新创建一些基本功能(如今在所有Internet浏览器中都可以看到).
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.
我认为:
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 :(
推荐答案
尝试如下操作:
tabControl1.Items.Insert(tabControl1.Items.Count-1,ti);
之所以这样做,是因为您总是至少有一个TabItem(即+)
This will do because you always have at least one TabItem (the + one)
然后选择倒数第二个
tabControl1.SelectedIndex=tabControl1.Items.Count-2;
这篇关于WPF C#以编程方式添加和移动选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!