Archive for July, 2011

Binding ItemsSource of TabControl in MVVM: No Tabs Visible

July 13, 2011

Once again, just to save you the same embarrassment, I will document a severe moment half-day of silliness that caused me a classic Journey Of Pain.

The short story was that I was binding my TabControl ItemsSource to an ObservableCollection but that no TabItems were being generated.

And the short story of the solution was that the ViewModel property containing the ObservableCollection was not public hence it was not accessible to the TabItem control.

Yep that’s right and that’s all. Since the ObservableCollection lives in another class (i.e. the ViewModel) to the View which access it, the access modifer needs to be suitably public. Yes, I know I am a churlish, dismal-dreaming fustilarian but before you gnaw your thumb at me, Sirrah just call to mind your latest coding embarrassment until you feel more chaitable. Finished? …. Good.

So, just to repeat that, if you are not seeing your ObservableCollection in your TabControl ItemsSource, make sure that the ViewModel property it is binding to is marked PUBLIC..

More Good News

You may not realise this, but Visual Studio by default will report WPF run-time Binding errors in your Debug Output Window. Mine looked like this:


System.Windows.Data Error: 40 : BindingExpression path error: 'TabReports' property not found on 'object' ''MainViewModel' (HashCode=32685253)'. BindingExpression:Path=TabReports; DataItem='MainViewModel' (HashCode=32685253); target element is 'TabControl' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

For more ways to debug WPF Binding errors go to this very useful page “Debugging Data Bindings in a WPF or Silverlight Application” written by your friend and mine at MSDN blogs karl140.6 (I notice he has recently upgraded himself). Brave man, karl and thank you.

Advertisements