WPF实现打开子窗体,并蒙版父窗体的问题。
很多代码都是这样的,可是这样打开的子窗体是点击不了的。请问什么情况呢。
整个程序关不掉了,只能在任务栏中关闭。像是只读了一样。
```c#
using System;
using System.Linq;
using System.Windows;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
namespace SG.Views.Suspend
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
/// <summary>
/// 点击打开子窗体的按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
this.ShowWindow(this, new Window2());
}
/// <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();
}
}
}
```