WPF在Frame中关闭UserControl并访问父控件

时间:2010-01-07 15:53:34

标签: wpf user-controls

我有一个带有工具栏和框架(ContentFrame)的Window(RootWindow)的WPF应用程序。最初隐藏工具栏。

我将一个Login UserControl加载到Frame中,当用户正确登录时,我想关闭UserControl,然后让Parent Window工具栏可见。

似乎这么简单。

但是,您无法从UserControl中关闭UserControl。那么我如何打破UserControl,以便我可以从RootWindow中删除它(ContentFrame.Source = Nothing),并使工具栏可见。

我可以使用以下代码获取父窗口的句柄,但我无法访问其中的控件

Dim parentWindow As Window = Window.GetWindow(Me) 'Get a handle for parent window

理想情况下,我希望能够从Child UserControl中访问父窗口控件,或者至少能够从子用户控件中触发父窗口中的事件。

1 个答案:

答案 0 :(得分:0)

要在雇佣军中找到父母,您可以使用以下代码:
http://www.hardcodet.net/2009/03/detecting-double-click-events-on-the-wpf-datagrid

虽然上面文章中解决的问题是针对DataGrid的,但是找到父代码的代码非常通用,应该适用于您的情况。