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

时间:2013-10-23 14:53:21

标签: c# wpf

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

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

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

1 个答案:

答案 0 :(得分:2)

试试这个:

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