Wpf 用户控件父对象左
所以我在一个窗口中有一个用户控件。我需要能够(从用户控制)检索左侧和顶部的父窗口(以便找到从子窗口打开的新弹出窗口)。我试图通过引用UserControl.Parent属性来实现这一点,但似乎不起作用Wpf 用户控件父对象左,wpf,Wpf,所以我在一个窗口中有一个用户控件。我需要能够(从用户控制)检索左侧和顶部的父窗口(以便找到从子窗口打开的新弹出窗口)。我试图通过引用UserControl.Parent属性来实现这一点,但似乎不起作用 有什么想法吗?谢谢 您正在使用MVVM吗?您是否关心在代码隐藏中编写代码。Net 3.5还是4.0 从后面的UserControl代码中,您可以使用: Window parentWindow = Window.GetWindow(userControlReference); public p
有什么想法吗?谢谢 您正在使用MVVM吗?您是否关心在代码隐藏中编写代码。Net 3.5还是4.0 从后面的UserControl代码中,您可以使用:
Window parentWindow = Window.GetWindow(userControlReference);
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
Loaded += new RoutedEventHandler(UserControl1_Loaded);
//Window parrentWindow = Window.GetWindow(this);//don't add here the value will be null
}
void UserControl1_Loaded(object sender, RoutedEventArgs e)
{
Window parrentWindow = Window.GetWindow(this);
}
}
不,实际上我可以触摸用户控件的代码隐藏,但不能触摸主窗口。那么我刚才发布的编辑应该可以在用户控件的代码隐藏中正常工作。谢谢:)