C# Application.Current.Windows.Cast<;窗口>;()返回null

C# Application.Current.Windows.Cast<;窗口>;()返回null,c#,wpf,C#,Wpf,我试图从另一个窗口访问一些控件(ScrollViewer和Grid)。我试过这个: var reportW = Application.Current.Windows.Cast<Window>().SingleOrDefault(window => window is ReportWindow) as ReportWindow; ScrollViewer myScrollViewer = reportW.testScrollViewer; Grid myGrid = repor

我试图从另一个窗口访问一些控件(ScrollViewer和Grid)。我试过这个:

var reportW = Application.Current.Windows.Cast<Window>().SingleOrDefault(window => window is ReportWindow) as ReportWindow;
ScrollViewer myScrollViewer = reportW.testScrollViewer;
Grid myGrid = reportW.Grd;
var reportW=Application.Current.Windows.Cast().SingleOrDefault(窗口=>window是ReportWindow)作为ReportWindow;
ScrollViewer myScrollViewer=reportW.testScrollViewer;
Grid myGrid=reportW.Grd;
问题是reportW总是
null
。我的方法有问题吗?是否有其他方法从另一个窗口访问控件?

尝试以下方法:

ReportWindow reportW = Application.Current.Windows.OfType<ReportWindow>().
SingleOrDefault();
ReportWindow reportW=Application.Current.Windows.OfType()。
SingleOrDefault();

还是一样的问题。我只是不知道怎么回事。我同意@Servy的说法,如果上面这一行返回
null
,那么您就没有类型为
ReportWindow
窗口。你确定
ReportWindow
是正确的类型吗?好吧,我找到了解决方案,但没有意识到我的窗口必须打开。因此,这种方法肯定行不通。但我需要在不打开窗口的情况下访问窗口控件。这可能吗?嘿,我找到了加载窗口而不显示它的解决方案。感谢您提示我必须打开“我的窗口”才能使用这一行代码。您的所有窗口都不是
ReportWindow
类型。请注意,此模式的几乎所有用途都是糟糕的设计。与其在所有打开的窗口中查找给定类型的窗口,不如在窗口之间建立更直接的关系。在这种情况下,该类型很可能要么返回值,要么触发事件,这样窗口就可以根据提供给它的信息进行自我更新。