title: Force binding in WPF
body: <p>I'm writing tests which will check correctness of <code>Binding</code> elements specified in XAML. They work so far, the only issue is that I do not know how to correctly force databinding to happen. Surprisingly it is not enough to simply set something in <code>DataContext</code>, binding won't happen until you show your control/window. Please not that I'm writing 'unit'-tests and I'd like to avoid showing any windows.</p>
  
  <p>Take a look at following code:  </p>
  
  <pre><code>// This is main class in console application where I have all WPF references added
  public class Program
  {
      [STAThread]
      public static void Main()
      {
          var view = new Window();
          BindingOperations.SetBinding(view, Window.TitleProperty, new Binding("Length"));
          view.DataContext = new int[5];
          //view.Show(); view.Close(); // &lt;-- this is the code I'm trying not to write
          Console.WriteLine(view.Title);
      }
  }
  </code></pre>
  
  <p>Here I'm creating a Window and putting an array as <code>DataContext</code> to that window. I'm binding <code>Window.Title</code> to <code>Array.Length</code> so I expect to see number <code>5</code> printed in console. But until I <code>Show</code> window (commented line) I will get empty string. If I uncomment that line then I will receive desired <code>5</code> in console output.  </p>
  
  <p>Is there any way I can make binding happen without showing a window? It is pretty annoying to look at ~20 windows while launching tests.</p>
  
  <p>P.S.: I know I can make windows more transparent and etc, but I'm looking for more elegant solution.</p>
  
  <p><strong>UPDATE</strong> Code above is simplified version of what I really have. In real code I receive a <code>View</code> (some <code>UIElement</code> with bindings) and <code>object ViewModel</code>. I do not know which exactly binding there were set on <code>View</code>, but I still want all of them to be initialized.  </p>
  
  <p><strong>UPDATE 2</strong>: Answering to the questions regarding what I test and I why. I do not intend to test that classes like <code>Binding</code>, <code>BindingBase</code>, etc are working as expected, I assume they are working. I'm trying to test that in all my XAML files I have written bindings correctly. Because bindings are stringly typed things, they are not verified during compilation and by default they cause only errors in output window, which I'm missing occasionally. So if we take my example from above and if we will made a typo there in binding: <code>{Binding Lengthhh}</code> then my tests will notify you that there is no property with name <code>Lengthhh</code> available for binding. So I have around 100 XAML files and for each XAML I have a test (3-5 lines of code) and after launching my tests I know <strong>for sure</strong> that there are no binding errors in my solution.</p>