Issue in Adding tab items at run time

2 posts, 0 answers
  1. Munish
    Munish avatar
    14 posts
    Member since:
    Apr 2013

    Posted 26 Feb 2014 Link to this post

    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.
  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 03 Mar 2014 Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top