打开子窗体
这是打开子窗体的代码,注释比较详细供大家参考
private void Button_Click(object sender, RoutedEventArgs e)
{
EditGateLIst gatel = new WpfApplication1.EditGateLIst();//这是我要打开的新窗体
//蒙板
Grid layer = new Grid() { Background = new SolidColorBrush(Color.FromArgb(200, 0, 0, 0)) };
//父级窗体原来的内容
UIElement original = MainWindows.Content as UIElement;//MainWindows父窗体
MainWindows.Content = null;
//容器Grid
Grid container = new Grid();
container.Children.Add(original);//放入原来的内容
container.Children.Add(layer);//在上面放一层蒙板
//将装有原来内容和蒙板的容器赋给父级窗体
MainWindows.Content = container;
//弹出消息框
gatel.Owner = MainWindows;
gatel.ShowDialog();
}
关闭子窗体
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//容器Grid
Grid grid = this.Owner.Content as Grid;
//父级窗体原来的内容
UIElement original = VisualTreeHelper.GetChild(grid, 0) as UIElement;
//将父级窗体原来的内容在容器Grid中移除
grid.Children.Remove(original);
//赋给父级窗体
this.Owner.Content = original;
}
本文详细介绍了在WPF应用中如何通过代码实现子窗体的弹出及优雅关闭,包括创建子窗体实例、使用蒙板效果、处理父窗体内容的保存与恢复,以及子窗体关闭时的事件监听。
810

被折叠的 条评论
为什么被折叠?



