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

Get Content Width of Inactive TabItem

2 Answers 477 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
Nathan
Top achievements
Rank 1
Nathan asked on 08 Nov 2017, 04:36 PM

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 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 13 Nov 2017, 01:07 PM
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.
0
Nathan
Top achievements
Rank 1
answered on 13 Nov 2017, 02:33 PM
The call to UpdateLayout is what I was missing, thanks.
Tags
TabControl
Asked by
Nathan
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Nathan
Top achievements
Rank 1
Share this question
or