R – WPF TabControl Switch behaviour

tabcontrolwpf

I have a tabcontrol which binds to an observable collection of tabs.

The tabcontrol always has the first tab which hosts a listview bound to another observable collection.

On selecting an item in that list view a new tab is created an focus given to it.

The problem I am having is:

  1. When I switch back to the first tab there is a pause while it redraws / creates the listview items (contains images so slow)

  2. The item selected before moving to the new tab is nolonger selected. Instead the listview is at the top with no item selected.

Can someone please explain to me how the tabcontrol operates is it really distroying the tab item content each time? and how I can instead have a behaviour where the item remains selected when I return to that tab?

Update:

I have confirmed by adding debug print messages to events that no events fire on this switch-back and forth but the first tab is being unloaded – more specifically the usercontrol hosted in that tab is??.

Best Answer

It sounds like the ObservableCollection is the culprit. If you are changing the collection items to control the display, then every time the collection changes won't it redraw the entire tab collection?

Instead, why not maintain the TabItem collection directly? You could then manage the Visibility property of the TabItems to display them or not.

Related Topic