1. 程式人生 > 其它 >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