tags:

views:

405

answers:

2

Hi,

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??.

A: 

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.

Joel Cochran
If this were the case I would think the first time I went back I would see the problem but not the second and subsequent switching from 1st to 2nd tab since the collection would be unchanged??
Oliver
A: 

First I needed to ensure my listview bound to my collection correctly i.e. the item stayed selected by adding the property:

IsSynchronizedWithCurrentItem="True"

I then added a loaded event handler to the listview so the item is scrolled into view on switching back:

private void ListView_Loaded(object sender, RoutedEventArgs e)
{
    ICollectionView collectionView = CollectionViewSource.GetDefaultView(DataContext);
    if (collectionView != null)
    {
        ItemControl.ScrollIntoView(collectionView.CurrentItem);
    }
}   
Oliver