Get Content Width of Inactive TabItem

3 posts, 0 answers
  1. Nathan
    Nathan avatar
    3 posts
    Member since:
    Oct 2017

    Posted 08 Nov 2017 Link to this post

    I have a window with a TabControl that has three TabItems. This TabControl is the main control in the window, and I want to size the window based on this TabControl.

    If I set SizeToContent="WidthAndHeight" on the window, the window changes size when I select a tab which has content that takes more area. I want the window to start at a fixed size based on the TabItem that will take the largest amount of space. This is not the starting tab.

    A TabItem's ActualWidth is the width of the button. The content of an inactive TabItem (a grid) has an ActualWidth of 0 and a Width of NaN because it is not being displayed.

    How can I set the window to a fixed size based on the content of the largest TabItem, which is not selected? This would preferably happen at Window_Loaded or Window_ContentRendered.

  2. Martin Ivanov
    Admin
    Martin Ivanov avatar
    2083 posts

    Posted 13 Nov 2017 Link to this post

    Hello Nathan,

    RadTabControl won't generate the tab's content until you select it. After you select another tab, the previous selected content will be destroyed and the new content will be loaded. You can alter this by setting the IsContentPreserved property of RadTabControl to True. But even if you set the property you will need to select the tabs at least once in order to load their content.

    The easiest way to achieve your requirement would be to set a equally fixed size to elements in each tab item. If this doesn't work for you it is possible to preload the content of the tabs by selecting them one by one. Note that in order for the content to get properly loaded and use its size you will need to call the tab's UpdateLayout() method.
    private void TabControl_Loaded(object sender, RoutedEventArgs e)
    {
        int originalSelection = this.tabControl.SelectedIndex;
        foreach (RadTabItem tab in this.tabControl.Items)
        {
            tab.IsSelected = true;
            tab.UpdateLayout();
        }
        this.tabControl.SelectedIndex = originalSelection;
    }
    You can find this approach shown in the attached project. Please give it a try and let me know if it helps.

    Regards,
    Martin Ivanov
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
  3. Nathan
    Nathan avatar
    3 posts
    Member since:
    Oct 2017

    Posted 13 Nov 2017 Link to this post

    The call to UpdateLayout is what I was missing, thanks.
Back to Top