wpf 打開與關閉 帶蒙板 的子窗體,

先上效果圖:

相關添加蒙板後臺代碼如下:

 /// <summary>
        /// 顯示子窗體
        /// </summary>
        /// <param name="obj">父窗體</param>
        /// <param name="showWindow">子窗體</param>
        private void ShowWindow(object obj, Window showWindow)
        {
            Window owner = null;
            if (obj is Window)
            {
                owner = obj as Window;
            }
            //蒙板
            Grid layer = new Grid() { Background = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0)) };
            //父級窗體原來的內容
            UIElement original = owner.Content as UIElement;
            owner.Content = null;
            //容器Grid
            Grid container = new Grid();
            container.Children.Add(original);//放入原來的內容
            container.Children.Add(layer);//在上面放一層蒙板
            //將裝有原來內容和蒙板的容器賦給父級窗體
            owner.Content = container;
            //彈出消息框 
            showWindow.Owner = owner; 
            showWindow.ShowDialog();
        }

退出時蒙板關閉子窗體關閉如下:

 /// <summary>
        /// 關閉窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            Grid grid = this.Owner.Content as Grid;
            //父級窗體原來的內容
            UIElement original = VisualTreeHelper.GetChild(grid, 0) as UIElement;
            //將父級窗體原來的內容在容器Grid中移除
            grid.Children.Remove(original);
            //賦給父級窗體
            this.Owner.Content = original;
            this.Close();
        }        

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章