Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make sure my WPF TabControl always has a selected tab when it contains at least one tab?

I have a TabControl whose items are bound to an ObservableCollection:

<TabControl ItemsSource="{Binding MyObservableCollection}" />

The tabs are added and removed as expected as items are added and removed from the collection. However, the SelectedItem reverts to -1 (meaning there is no selected tab) whenever the collection is empty. Then, when an item is added, the SelectedItem stays at -1 and the new tab is not selected.

How do I make the TabControl select the new tab whenever an item is added to the empty collection?

like image 523
GraemeF Avatar asked Jul 24 '09 11:07

GraemeF


1 Answers

There might be an easier way, but you could hook the collection changed event on the ObservableCollection in your VM and set the SelectedItem property to the new item (assuming you have the selected item bound to a property on the VM).

like image 194
Steven Robbins Avatar answered Oct 07 '22 21:10

Steven Robbins