title: How can I get a control's DataContext from an external application using UI Automation (and/or White)
body: <p>I'm using <a href="http://white.codeplex.com/" rel="nofollow noreferrer">White</a> to drive the UI of a WPF app, and it's worked well so far.</p>
  
  <p>I'm at the point however, where I need to poke into the internal state of the app under test to check some conditions.</p>
  
  <p>Specifically, I have a DataGrid (from the <a href="http://wpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29117" rel="nofollow noreferrer">WPFToolkit</a>), which is databound to a <code>List&lt;MyBusinessObject&gt;</code>. Each row therefore has a <code>DataContext</code> of a <code>MyBusinessObject</code></p>
  
  <p>I can get the grid using white by finding it with it's automation ID.</p>
  
  <p>I can then do this:</p>
  
  <pre><code>var row = Grid.Rows[0]
  row.AutomationElement.Current.Name
  </code></pre>
  
  <p>which returns the string <code>"MyCompany.Namespace.MyBusinessObject"</code>, so I'm <em>almost</em> there, but I can't actually get the actual business object itself.</p>
  
  <p>I've looked into WPF UIAutomation AutomationPeers (from an <code>AutomationPeer</code>, you can get the <code>Owner</code> property, which is the actual business object), but it seems that you need to be in-process to use these, as they all have constructors which require you to supply the WPF control.</p>
  
  <p>Is there any way I can construct an AutomationPeer from an external process, or otherwise drill down into the actual <code>DataContext</code> of a WPF control? </p>