红着眼 2023-11-22 10:03 采纳率: 27.8%
浏览 16
已结题

WPF蒙版子窗体后,整个窗体无法点击。

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

```

  • 写回答

13条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-11-22 10:11
    关注

    引自强大而免费的微信小程序:皆我百晓生

    在这段代码中,蒙版是通过添加一个半透明的Grid来实现的。当点击打开子窗体的按钮后,会调用ShowWindow方法来显示子窗体。

    问题分析:

    打开的子窗体无法点击,整个窗体也无法关闭。

    原因分析:

    这个问题可能是由于蒙版层覆盖在整个窗体上导致的。由于蒙版是一个占据整个窗体的Grid,它会拦截所有鼠标和键盘事件,导致无法与子窗体进行交互。

    在调用ShowDialog方法显示子窗体时,使用了Owner属性来指定父窗体,这会导致子窗体无法获取焦点和交互。

    解决方案:

    一种解决方案是将蒙版添加到子窗体上而不是父窗体。这样可以避免蒙版拦截父窗体的交互事件。可以在子窗体的Loaded事件中添加蒙版,并在关闭子窗体时移除蒙版。

    以下是修复后的代码:

    using System;
    using System.Linq;
    using System.Windows;
    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)
            {
                var window2 = new Window2();
                window2.Owner = this;
                this.AddOverlay();
                window2.Closed += (s, args) => this.RemoveOverlay();
                window2.ShowDialog();
            }
    
            /// <summary>
            /// 添加蒙版
            /// </summary>
            private void AddOverlay()
            {
                var overlay = new Grid() { Background = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0)) };
                this.Content = null;
                var container = new Grid();
                container.Children.Add(this.Content as UIElement);
                container.Children.Add(overlay);
                this.Content = container;
            }
    
            /// <summary>
            /// 移除蒙版
            /// </summary>
            private void RemoveOverlay()
            {
                if (this.Content is Grid container && container.Children.Count == 2 && container.Children[1] is Grid overlay)
                {
                    this.Content = container.Children[0]; // 恢复原来的内容
                }
            }
        }
    }
    

    在修复后的代码中,我将蒙版的逻辑移到了子窗体的事件中。在点击打开子窗体的按钮后,我首先添加蒙版,然后将其移除。

    这样修复后,打开的子窗体不会拦截父窗体的交互事件,同时能够正常关闭整个程序。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(12条)

报告相同问题?

问题事件

  • 系统已结题 12月5日
  • 已采纳回答 11月27日
  • 赞助了问题酬金15元 11月22日
  • 创建了问题 11月22日

悬赏问题

  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 关于#c语言#的问题:我在vscode和codeblocks中编写c语言时出现打不开源文件该怎么办
  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表
  • ¥15 Altair EDEM中生成一个颗粒,并且各个方向没有初始速度
  • ¥15 系统2008r2 装机配置推荐一下
  • ¥500 服务器搭建cisco AnyConnect vpn
  • ¥15 悬赏Python-playwright部署在centos7上
  • ¥15 psoc creator软件有没有人能远程安装啊
  • ¥15 快速扫描算法求解Eikonal方程咨询