我的应用程序有几个独立的“顶级”窗口,它们都具有完全不同的功能/工作流程。
我目前正在使用ShowDialog()来创建一个WPF窗口模态。模态窗口是主窗口之一的小孩。但是,一旦它打开,它将阻止所有的顶级窗口。我希望对话框只阻止从它启动的父窗口。这可能吗?
我不知道是否重要,但打开对话框的窗口是应用程序的初始窗口,因此所有其他顶级窗口都从该窗口打开。
一个选项是启动不想要在不同线程上对话框影响的窗口。这可能会导致您的应用程序出现其他问题,但如果这些窗口确实封装了不同的工作流程,那可能不是问题。这是我写的一些示例代码,以验证它是否有效:
<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; } }
我采用this blog post在不同的线程上运行WPF窗口。