Application.current.mainwindow是childwindow的替代方案

时间:2013-02-02 01:37:13

标签: c# wpf mainwindow

我想使用以下代码

Point point1 = pwImages.ElementAt(0).TransformToAncestor(Application.Current.MainWindow).Transform(new Point(0, 0));

但是,Application.Current.MainWindow未设置为调用此代码的窗口,因为mainWindow是app.xaml调用的窗口。除了Application.Current.MainWindow之外,还有其他我可以使用的替代方法,还是可以将Application.Current.MainWindow重置为我当前的窗口?

1 个答案:

答案 0 :(得分:0)

好的,如果我发现我需要遍历我的子窗口列表以找到我正在寻找的窗口并将其分配给变量。

 Window myWindow;
   foreach (Window objWindow in Application.Current.Windows)
        {
            if (objWindow.ToString().Equals("NameOfChildWindow"))
            {
                myWindow = objWindow;
            }
        }

然后我可以使用objWindow作为替换属性

  Point point1 = pwImages.ElementAt(0).TransformToAncestor(myWindow).Transform(new Point(0, 0));