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

时间:2021-08-19
本文章向大家介绍WPF打开子窗口给父窗口添加蒙版效果,主要包括WPF打开子窗口给父窗口添加蒙版效果使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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

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;
        }

  本文引用自CSDN

原文地址:https://www.cnblogs.com/zunzunQ/p/15162982.html