Force binding in WPF Force binding in WPF wpf wpf

Force binding in WPF


The bindings are updated by the dispatcher with the DispatcherPriority.DataBind - so if you wait for a dummy task with SystemIdle priority you are sure that any pending databinding is done.

  try  {    this.Dispatcher.Invoke(DispatcherPriority.SystemIdle, new Action(() => { }));  }  catch  {    // Cannot perfom this while Dispatcher in suspended mode  }


If you are trying to test correctness of your view, I suggest you test your view :-)

Why not run the UI from a unit test and write code that checks content of UI after changing data.

VS2010 does have GUI testing, or you could take a look at the code of tools such as Snoop.


Edit following comment:

If ALL you want to do is test a few simple bindings, try writing a static code test that runs as a post build event using reflection on view models and regular expressions on XAMLs. Add attributes on VM or use a config file so your test will know which view receives which View Model as DataContext. Compare property names and types in View Models with binding strings in View (automatically search XAML for these) and throw exception (thus failing build) if strings do not match.

If your bindings are more complex (converters, multibindings, ...) this may be a bit more complicated to implement.


I think you should first set the DataContext and then do the Binding, e.g.:

view.DataContext = new int[5];BindingOperations.SetBinding(view, Window.TitleProperty, new Binding("Length"));

I'm not sure if this is real solution for your general problem, but it works in this case.