This is a migrated thread and some comments may be shown as answers.

Issue in Adding tab items at run time

1 Answer 169 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
Munish
Top achievements
Rank 1
Munish asked on 26 Feb 2014, 11:30 AM
Hi Team

I have a radtabcontrol.Items source bind to some collection in ViewModel. Each tab item has its own view and VM. I am adding multiple tabitems to the control at run time. Steps are :
1. create the instance of the view model of the tab item
2. Add it to the collection in the VM.

After this , the tabcontrol shows the desired tabs. The issue is that the view of only last tab item is initialised , other tab items views remain uninitialised.  I need to select each tab item one by one...only then the view is getting initialised.

I need to read some control info from each tab item view model...for that i need each view to be initialised without manually clicking them one by one.

1 Answer, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 03 Mar 2014, 09:24 AM
Hi Munish,

RadTabControl uses lazy loading which basically means that it loads the content of its tabs only when these tabs are selected. Additionally the component destroys the content of an unselected tab whenever the selection changes. These are the reasons behind the behavior you described.

You can easily stop the RadTabControl from destroying the content of unselected tabs using the IsContentPreserved property further described in the How to Keep the Content State tutorial.

However, there is not an easy way to make the component load all RadTabItem Contents on load and if this is what you're looking for, then I would suggest as a workaround to manually traverse the RadTabControl.Items collection and selected each tab once so that it content can load:
public void TabControl_Loaded(object sender, RoutedEventArgs e)
{
    var tabControl = sender as RadTabControl;
    var orgIndex = tabControl.SelectedIndex;
    foreach (RadTabItem item in tabControl.Items)
    {
        if (item.IsSelected)
        {
 
            continue;
        }
        item.IsSelected = true;
        TabControl.UpdateLayout();
    }
    tabControl.SelectedIndex = orgIndex;
}

I attached a sample solution for your convenience. Please give this approach a try and let me know if it helps.

Regards,
Tina Stancheva
Telerik
Tags
TabControl
Asked by
Munish
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Share this question
or