Wednesday, November 21, 2012

Check if controls on another wpf window have rendered using thread

Given you have two WPF windows namely MainWindow and OtherWindow. You basically want to check if the controls in main window have successfully loaded using statements in OtherWindow. The solution is to use Application dispatcher object.
Code:
if(Application.Current.Dispatcher.CheckAccess())
        {  
          while (true)  
          {  
            foreach (Window window in Application.Current.Windows)  
            {  
              if (window.Title.Equals("Project Title"))  
              {  
                if (window.FindName("your_control") != null)  
                {  
                  //do your stuff here...  
                }  
              }  
            }  
            break;  
          }  
        }  
        else  
        {  
          Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(CheckMainWindow));  
        }  
Cheers!

0 comments:

Post a Comment