Application.Current.Windows.Cast<Window>() 返回 null



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

var reportW = Application.Current.Windows.Cast<Window>().SingleOrDefault(window => window is ReportWindow) as ReportWindow;
ScrollViewer myScrollViewer = reportW.testScrollViewer;
Grid myGrid = reportW.Grd;

问题是报告W是所有null。我的方法有问题吗,还有其他方法可以从另一个窗口访问控件吗?

试试这个:

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

最新更新