一、先造一个窗体,然后在窗体里面增加菜单栏及原始的tabcontrol选项卡
<grid>
<menu>
<menuitem header="文件" click="menuitem_click_3">
<menuitem header="打开新窗口" click="menuitem_click_1"></menuitem>
<menuitem header="退出" click="menuitem_click_4" ></menuitem>
</menuitem>
<menuitem header="帮助">
<menuitem header="关于我们"></menuitem>
</menuitem>
</menu>
<tabcontrol name="tabcontrol1" horizontalalignment="left" height="337" margin="0,20,0,0" verticalalignment="top" width="572">
<tabitem header="原始窗口">
<grid background="#ffe5e5e5">
<textbox horizontalalignment="left" height="305" margin="0" textwrapping="wrap" text="" verticalalignment="top" width="562"/>
</grid>
</tabitem>
</tabcontrol>
</grid>
二、当点击打开新窗口,便弹出新窗口,并且在新窗口里增加一个占满的文本框
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private int i = 0;//定义一个i用来使新造的Item名字不同
TextBox t;//每造一个新Item 然后将Textbox扔进去
private void MenuItem_Click_1(object sender, RoutedEventArgs e) //打开新窗口按钮
{
TabItem ti = new TabItem();//造一个新选项卡
ti.Header = "新窗口"+(i+1);//新选项卡的名字
TabControl1.Items.Add(ti);//将造好的新选项卡扔进TabControl1里
t = new TextBox();//实体化一个新文本框
t.Width = 562;
t.Height = 305;
//下面开始将文本框添加到新造的选项卡中
ti.Content = t;
//每造一个新窗口便默认突出显示为新窗口
TabControl1.SelectedIndex = i+1; //TabControl1.SelectedIndex = TabControl1.Items.Count - 1;
i++; //双击每个选项卡触发的事件 ti.MouseDoubleClick+=ti_MouseDoubleClick;
}
private void MenuItem_Click_4(object sender, RoutedEventArgs e)//关闭按钮
{
this.Close();
}
private void ti_MouseDoubleClick(object sender, MouseButtonEventArgs e) { TabItem s = (TabItem)sender; //sender代表每个选项卡 TabControl1.Items.Remove(s); }
private void MenuItem_Click_3(object sender, RoutedEventArgs e)
{
}
}