title: When accessing a UI element from another class using dispatcher, the UI element is null
body: <p>I have a class which needs to update my UI. Currently I am calling a function held in the UI class and passing through the what needs to be updated.
  eg</p>
  
  <pre><code>public class someclass
  {
      public someclass()
      {
          MainWindow main = new MainWindow();
          main.DrawGrid(Brushes.Black);
      }
  }
  
  public class MainWindow
  {
  ...
      public void DrawGrid(Brush colour)
      {
          ReturnGrid().background = colour;
      }
  }
  </code></pre>
  
  <p>But this has issues with threading, so I updated my code to use the dispatcher.</p>
  
  <pre><code>public class someclass
  {
      public someclass()
      {
          MainWindow main = new MainWindow();
          main.DrawGrid(string "black");
      }
  }
  
  public class MainWindow
  {
  ...
      public void DrawGrid(Brush colour)
      {
          Dispatcher.Invoke(() =&gt; {ReturnGrid().Background = colour};);
      }
      public Grid ReturnGrid()
      {
          return maingrid; //defined in xaml
      }
  }
  </code></pre>
  
  <p>However this throws up an error that the returned grid is null.
  However if I called DrawGrid from MainWindow it would work fine.</p>
  
  <p>EDIT: Data binding approach</p>
  
  <pre><code>public class someclass
  {
      public someclass()
      {
          MainWindow.testbackground = Brushes.Black;
      }
  }
  
  public class MainWindow
  {
  public static Brush testbackground = Brushes.White;
  ...
  }
  &lt;MainWindow x:Name="mainwindow" ...&gt;
  ...
  &lt;Grid ... Background="{Binding testbackground ElementName=mainwindow}"/&gt;
  &lt;/MainWindow&gt;
  </code></pre>