WPF 从 UserControl 打开模态窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14851635/
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
WPF Open a Modal window from UserControl
提问by Nollaig
I have a MainWindow in my WPF app.
我的 WPF 应用程序中有一个 MainWindow。
This MainWindow has a menu on the left and when a menu option is selected a UserControl is loaded on the right. This is similar to Master Pages in asp.net
This MainWindow has a menu on the left and when a menu option is selected a UserControl is loaded on the right. 这类似于 asp.net 中的母版页
What I want to do now is to have a modal window show from the UserControl which will only allow the user to interact with the modal window.
我现在想要做的是从 UserControl 显示一个模态窗口,它只允许用户与模态窗口进行交互。
I have seen examples of the Main Window showing a modal window (http://www.codeproject.com/Articles/36516/WPF-Modal-Dialog) but not sure on how to load this from a UserControl.
我已经看到了主窗口的示例,显示了一个模式窗口(http://www.codeproject.com/Articles/36516/WPF-Modal-Dialog),但不确定如何从 UserControl 加载它。
回答by Ian
There's this: dialogs and mvvmbut this is the best example I've seen of dealing with it: mvvm and closing forms
有这个:对话框和 mvvm但这是我见过的最好的例子:mvvm 和关闭表单
The first link I've not used and stumbled across while looking for the second link to post that here. The second link has two downloads, you can ignore the _service download, it's basically the same.
我在寻找第二个链接时偶然发现的第一个链接在这里发布。第二个链接有两个下载,可以忽略_service下载,基本一样。
回答by ΩmegaMan
One way in WPF is this method
WPF 中的一种方法是这种方法
- Add a new
Windowto the project. - Add other controls onto the window as needed.
- In XAML name the window such as
x:Name="MyWindow" - Put on Dependency properties on the window and have each of the controls bind to the window's data context such as
{Binding MyText, ElementName=MyWindow}. (Note I still use, even for WPF these Visual Studio code snippets to add different dependency properties, these save time for a very redundant operations of adding them: Silverlight Snippets. - In the location where you want to launch the model dialog use this example:
Window向项目添加一个新项目。- 根据需要将其他控件添加到窗口中。
- 在 XAML 中命名窗口,例如
x:Name="MyWindow" - 在窗口上放置 Dependency 属性,并将每个控件绑定到窗口的数据上下文,例如
{Binding MyText, ElementName=MyWindow}. (注意,即使对于 WPF,我仍然使用这些 Visual Studio 代码片段来添加不同的依赖项属性,这些为添加它们的非常冗余的操作节省了时间:Silverlight Snippets。 - 在您要启动模型对话框的位置使用此示例:
Example:
例子:
var about = new About(); // Create the new window
// I've added a CompanyName dependency property.
about.CompanyName = "OmegaMan Industries";
about.ShowDialog();

