wpf 開啟與關閉 帶蒙板 的子窗體,
阿新 • • 發佈:2019-02-02
先上效果圖:
相關新增蒙板後臺程式碼如下:
/// <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(); }