WPF打开子窗口给父窗口添加蒙版效果

2024-04-23 19:09:24 浏览数 (2)

打开子窗体

这是打开子窗体的代码,注释比较详细供大家参考

代码语言:javascript复制
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();
        }

关闭子窗体

代码语言:javascript复制
 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;
        }

0 人点赞