WPF——菜单栏及TabControl

一、先造一个窗体,然后在窗体里面增加菜单栏及原始的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)
        {

        }
    }

posted @ 2015-06-29 22:40  XCml  阅读(33975)  评论(1编辑  收藏  举报