2012-08-27 56 views
1

我在WPF窗口的Canvas下有一些通用User Control如何在UserControl中获取主窗口

我该如何获得该窗口?

我用这种方法

void UserControlMethod() 
{  
    WindowInteropHelper WindowInteropHelper = new WindowInteropHelper((Window)((Grid)(((Canvas)this.Parent).Parent)).Parent);  
} 

这是正确的吗?

谢谢!

回答

3

您应该使用:Window.GetWindow()

WindowInteropHelper是用于与本机窗口代码或API的交互,所以这绝对不是你想要的。

+0

明白了。它应该是WindowInteropHelper windowInteropHelper = new WindowInteropHelper(Window.GetWindow(this)); –

+0

你在做一些Win32 API调用吗? – CodingGorilla

+0

谢谢你的建议是正确的! –