.net 如何在 WPF 中制作模态对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/499294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make modal dialog in WPF?
提问by Alex Baranosky
I am writing my first application in WPF and want to have the user input some data on a modal dialog window. Apparently, this is not simple to do in WPF, because the parent window stays fully enabled, and the method that created the new child window doesn't stop and wait for the child window to call Close(). Instead it just keeps going forward. This is not what I want.
我正在用 WPF 编写我的第一个应用程序,并希望用户在模态对话框窗口中输入一些数据。显然,这在 WPF 中做起来并不简单,因为父窗口保持完全启用状态,并且创建新子窗口的方法不会停止并等待子窗口调用 Close()。相反,它只是继续前进。这不是我想要的。
How can I get the child window to open, and have the parent window wait for the child to close before the parent window continues executing?
如何让子窗口打开,并让父窗口在父窗口继续执行之前等待子窗口关闭?
回答by Yordan Pavlov
Did you try showing your window using the ShowDialogmethod?
您是否尝试使用ShowDialog方法显示您的窗口?
Don't forget to set the Owner propertyon the dialog window to the main window. This will avoid weird behavior when Alt+Tabbing, etc.
不要忘记将对话窗口上的Owner 属性设置为主窗口。这将避免 Alt+Tabbing 等时的奇怪行为。
回答by vapcguy
A lot of these answers are simplistic, and if someone is beginning WPF, they may not know all of the "ins-and-outs", as it is more complicated than just telling someone "Use .ShowDialog()!". But that is the method (not .Show()) that you want to use in order to block use of the underlying window and to keep the code from continuing until the modal window is closed.
很多这些答案都很简单,如果有人开始使用 WPF,他们可能不知道所有的“来龙去脉”,因为这比告诉某人“使用.ShowDialog()!”要复杂得多。但这是.Show()您想要使用的方法 (not ),以阻止使用底层窗口并防止代码继续,直到模式窗口关闭。
First, you need 2 WPF windows. (One will be calling the other.)
首先,您需要 2 个 WPF 窗口。(一个会打电话给另一个。)
From the first window, let's say that was called MainWindow.xaml, in its code-behind will be:
从第一个窗口开始,假设它被称为 MainWindow.xaml,其代码隐藏将是:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
Then add your button to your XAML:
然后将您的按钮添加到您的 XAML:
<Button Name="btnOpenModal" Click="btnOpenModal_Click" Content="Open Modal" />
And right-click the Clickroutine, select "Go to definition". It will create it for you in MainWindow.xaml.cs:
并右键单击该Click例程,选择“转到定义”。它将在 MainWindow.xaml.cs 中为您创建它:
private void btnOpenModal_Click(object sender, RoutedEventArgs e)
{
}
Within that function, you have to specify the other page using its page class. Say you named that other page "ModalWindow", so that becomes its page class and is how you would instantiate (call) it:
在该函数中,您必须使用其页面类指定另一个页面。假设您将另一个页面命名为“ModalWindow”,以便它成为它的页面类,并且您将如何实例化(调用)它:
private void btnOpenModal_Click(object sender, RoutedEventArgs e)
{
ModalWindow modalWindow = new ModalWindow();
modalWindow.ShowDialog();
}
Say you have a value you need set on your modal dialog. Create a textbox and a button in the ModalWindowXAML:
假设您需要在模态对话框中设置一个值。在ModalWindowXAML 中创建一个文本框和一个按钮:
<StackPanel Orientation="Horizontal">
<TextBox Name="txtSomeBox" />
<Button Name="btnSaveData" Click="btnSaveData_Click" Content="Save" />
</StackPanel>
Then create an event handler (another Clickevent) again and use it to save the textbox value to a public static variable on ModalWindowand call this.Close().
然后Click再次创建一个事件处理程序(另一个事件)并使用它来将文本框值保存到公共静态变量 onModalWindow并调用this.Close().
public partial class ModalWindow : Window
{
public static string myValue = String.Empty;
public ModalWindow()
{
InitializeComponent();
}
private void btnSaveData_Click(object sender, RoutedEventArgs e)
{
myValue = txtSomeBox.Text;
this.Close();
}
}
Then, after your .ShowDialog()statement, you can grab that value and use it:
然后,在您的.ShowDialog()声明之后,您可以获取该值并使用它:
private void btnOpenModal_Click(object sender, RoutedEventArgs e)
{
ModalWindow modalWindow = new ModalWindow();
modalWindow.ShowDialog();
string valueFromModalTextBox = ModalWindow.myValue;
}
回答by Dominic Hopton
Window.Show Window will show the window, and continue execution -- it's a non-blocking call.
Window.Show Window 将显示窗口,并继续执行——这是一个非阻塞调用。
Window.ShowDialog will block the calling thread (kinda [1]), and show the dialog. It will also block interaction with the parent/owning window. When the dialog is dismissed (forwhatever reason) ShowDialog will return to the caller, and will allow you to access DialogResult (if you want it).
Window.ShowDialog 将阻塞调用线程(有点 [1]),并显示对话框。它还将阻止与父/拥有窗口的交互。当对话框被关闭时(无论出于何种原因),ShowDialog 将返回给调用者,并允许您访问 DialogResult(如果需要)。
[1] It will keep the dispatcher pumping by pushing a dispatcher frame onto the WPF dipatcher. This will cause the message pump to keep pumping.
[1] 它将通过将调度器框架推送到 WPF 分配器上来保持调度器抽水。这将导致消息泵继续泵送。
回答by harms
Given a Window object myWindow, myWindow.Show() will open it modelessly and myWindow.ShowDialog() will open it modally. However, even the latter doesn't block, from what I remember.
给定一个 Window 对象 myWindow,myWindow.Show() 将无模式地打开它,而 myWindow.ShowDialog() 将模态地打开它。但是,即使是后者也不会阻止,据我所知。

