Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 用户控件父对象左_Wpf - Fatal编程技术网

Wpf 用户控件父对象左

Wpf 用户控件父对象左,wpf,Wpf,所以我在一个窗口中有一个用户控件。我需要能够(从用户控制)检索左侧和顶部的父窗口(以便找到从子窗口打开的新弹出窗口)。我试图通过引用UserControl.Parent属性来实现这一点,但似乎不起作用 有什么想法吗?谢谢 您正在使用MVVM吗?您是否关心在代码隐藏中编写代码。Net 3.5还是4.0 从后面的UserControl代码中,您可以使用: Window parentWindow = Window.GetWindow(userControlReference); public p

所以我在一个窗口中有一个用户控件。我需要能够(从用户控制)检索左侧和顶部的父窗口(以便找到从子窗口打开的新弹出窗口)。我试图通过引用UserControl.Parent属性来实现这一点,但似乎不起作用


有什么想法吗?谢谢

您正在使用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);
    }
}

不,实际上我可以触摸用户控件的代码隐藏,但不能触摸主窗口。那么我刚才发布的编辑应该可以在用户控件的代码隐藏中正常工作。谢谢:)