使用 ShowDialog() 的 WPF 模态窗口阻止所有其他窗口 [英] WPF Modal Window using ShowDialog() Blocks All Other Windows

查看:42
本文介绍了使用 ShowDialog() 的 WPF 模态窗口阻止所有其他窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有几个独立的顶级"窗口,它们都有完全不同的功能/工作流程.

My application has several independent "top-level" windows, which all have completely different functions/workflows.

我目前正在使用 ShowDialog() 制作 WPF 窗口模式.模态窗口是主窗口之一的子窗口.但是,一旦打开,它就会阻止所有顶级窗口.我希望对话框只阻止它启动的父窗口.这可能吗?

I am currently using ShowDialog() to make a WPF Window modal. The modal window is a child of one of the main windows. However, it is blocking all the top-level windows once it is open. I would like the dialog to block ONLY the parent window it was launched from. Is this possible?

我不确定这是否重要,但打开对话框的窗口是应用程序的初始窗口——因此所有其他顶级窗口都是从中打开的.

I'm not sure if it matters, but the window that opens the dialog is the initial window of the app--so all other top-level windows are opened from it.

推荐答案

一种选择是在不同线程上启动您不希望受对话框影响的窗口.这可能会导致您的应用程序出现其他问题,但如果这些窗口确实封装了不同的工作流,那可能就不是问题.这是我编写的一些示例代码来验证它是否有效:

One option is to start the windows that you don't want affected by the dialog on a different thread. This may result in other issues for your application, but if those windows do really encapsulate different workflows, that may not be an issue. Here is some sample code I wrote to verify that this works:

<Window x:Class="ModalSample.MyWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="{Binding Identifier}" Height="150" Width="150">
    <StackPanel>
        <TextBox Text="{Binding Identifier}" />
        <Button Content="Open Normal Child" Click="OpenNormal_Click" />
        <Button Content="Open Independent Child" Click="OpenIndependent_Click" />
        <Button Content="Open Modal Child" Click="OpenModal_Click" />
    </StackPanel>
</Window>

<小时>

using System.ComponentModel;
using System.Threading;
using System.Windows;

namespace ModalSample
{
    /// <summary>
    /// Interaction logic for MyWindow.xaml
    /// </summary>
    public partial class MyWindow : INotifyPropertyChanged
    {
        public MyWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        private int child = 1;

        private string mIdentifier = "Root";
        public string Identifier
        {
            get { return mIdentifier; }
            set
            {
                if (mIdentifier == value) return;
                mIdentifier = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Identifier"));
            }
        }

        private void OpenNormal_Click(object sender, RoutedEventArgs e)
        {
            var window = new MyWindow {Identifier = Identifier + "-N" + child++};
            window.Show();
        }

        private void OpenIndependent_Click(object sender, RoutedEventArgs e)
        {
            var thread = new Thread(() =>
                {
                    var window = new MyWindow {Identifier = Identifier + "-I" + child++};
                    window.Show();

                    window.Closed += (sender2, e2) => window.Dispatcher.InvokeShutdown();

                    System.Windows.Threading.Dispatcher.Run();
                });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }

        private void OpenModal_Click(object sender, RoutedEventArgs e)
        {
            var window = new MyWindow { Identifier = Identifier + "-M" + child++ };
            window.ShowDialog();
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

我来源于这篇博文 用于在不同线程上运行 WPF 窗口.

I sourced this blog post for running a WPF window on a different thread.

这篇关于使用 ShowDialog() 的 WPF 模态窗口阻止所有其他窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆