wpf 点击新建弹出新的窗口

时间:2024-04-14 08:15:22

wpf 点击新建弹出新的窗口

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void TabControl_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {

        }
        private int a;
        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            TabItem b = new TabItem();//先造一个新的选项
            b.Header = "窗口" + a;//新建一个名字,如窗口2
            aaa.Items.Add(b);//把建好的放到里面去
            aaa.SelectedIndex = a + 1;//造新窗口
            a++;//递增

        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            this.Close();//把按钮关闭
        }

      
}


    }
}
<Window x:Class="wpf_点击新建弹出新的窗口.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="窗口" Height="350" Width="525">
    <Grid>
        <Menu>
            <MenuItem Header="文件"> 
            <MenuItem Header="新建窗口" Click="MenuItem_Click_1"></MenuItem>
            <MenuItem Header="关闭" Click="MenuItem_Click_2"></MenuItem>
            </MenuItem>
            <MenuItem Header="帮助">
            </MenuItem>
            <TabControl Name="aaa" HorizontalAlignment="Left" Height="299" Width="518">
                <TabItem Header="窗口" >
                    <Grid Background="#FFE5E5E5"/>
                </TabItem>
                
            </TabControl>
        </Menu>
    </Grid>
</Window>