如何使用 WPF 用户控件关闭父窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32865659/
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 close parent windows using WPF User Control
提问by Darshan Faldu
Assume that I have two WPF windows: window_One and window_Two.
假设我有两个 WPF 窗口:window_One 和 window_Two。
- window_One has one button. Clicking this button opens window_Two.
- window_Two contains a User Control.
- This user control has one button that closes window_Two.
- window_One 有一个按钮。单击此按钮将打开 window_Two。
- window_Two 包含一个用户控件。
- 这个用户控件有一个关闭 window_Two 的按钮。
How can I achieve this scenario?
我怎样才能实现这个场景?
回答by Karl Patrik Johansson
Inside the custom control that you've created. You can access the parent window from within the button event click.
在您创建的自定义控件中。您可以从按钮事件单击中访问父窗口。
Either by using the visual tree:
通过使用可视化树:
var myWindow = (Window)VisualParent.GetSelfAndAncestors().FirstOrDefault(a => a is Window);
myWindow.Close();
or by simply:
或简单地:
var myWindow = Window.GetWindow(this);
myWindow.Close();
Of course, the other option would be to create a custom event that says "MyButtonClicked" and then have the window that hosts the UserControl to listen to this event and when the Event is fired, you close the current window.
当然,另一种选择是创建一个显示“MyButtonClicked”的自定义事件,然后让承载 UserControl 的窗口监听此事件,当事件被触发时,您关闭当前窗口。
Cheers!
干杯!
回答by cscmh99
You can try EventAggregator to implement this event driven logic across different ViewModel.
您可以尝试使用 EventAggregator 来跨不同的 ViewModel 实现此事件驱动逻辑。
http://www.codeproject.com/Articles/355473/Prism-EventAggregator-Sample
http://www.codeproject.com/Articles/355473/Prism-EventAggregator-Sample
回答by vapcguy
We implemented this to close Window1 from Window2 getting opened, but it should work in any scenario to close any window from anywhere if you place these code parts in their appropriate areas:
我们实现了这一点以关闭 Window1 从 Window2 被打开,但如果您将这些代码部分放在适当的区域,它应该可以在任何情况下从任何地方关闭任何窗口:
Create a class that stores a Windowobject, and a function that will close it:
创建一个存储Window对象的类和一个关闭它的函数:
CloseWindow.cs
关闭窗口.cs
public static class CloseWindow
{
public static Window WinObject;
public static void CloseParent()
{
try
{
((Window)WinObject).Close();
}
catch (Exception e)
{
string value = e.Message.ToString(); // do whatever with this
}
}
}
In the parent window (window you want to close - Window2, in this case?), in its onload event, set its Windowobject equal to CloseWindow.WinObject:
在父窗口(您要关闭的窗口 - Window2,在这种情况下?),在其 onload 事件中,将其Window对象设置为等于CloseWindow.WinObject:
CloseWindow.WinObject = (Window)this;
Then, in the child's onload event (or, in the case of the OP, in Window2's User Control's button event), have it perform the CloseParent()function:
然后,在孩子的 onload 事件中(或者,在 OP 的情况下,在 Window2 的用户控件的按钮事件中),让它执行以下CloseParent()功能:
if (CloseWindow.WinObject != null)
CloseWindow.CloseParent();

