Note that there are some explanatory texts on larger screens.

plurals
  1. PORemember tab size where the tabs have been resized
    text
    copied!<p>I have a tab control in a window with several tabs on it.</p> <p>By default the size of the window resizes around the TabControl which is sized to the contents of the TabItems. This is desired behaviour. When one tab is resized all the tabs become that size. Presumably this is because it is the window that is being resized. However the desired behaviour is for the window to size to the tab unless that tab has been resized. If that tab has been resized its size needs to be remembered. I have been unable to implement this correctly, usually it will resize the first time but when the tab is set to again and resized it appears that only the window resizes and not the tabs.</p> <p>Additionally I am unable to hard code the sizes as the controls on the TabItems are dynamically created and will not always be the same size. Some of these are WPF and some are WinForms controls in a WindowsFormHost. There are about a dozen of these but just using 3 in my example code. One is on a scroll viewer.</p> <pre><code>&lt;Window.... SizeToContent="WidthAndHeight" ResizeMode="CanResize"&gt; &lt;Grid&gt; &lt;TabControl x:Name="tabControl" SizeChanged="tabControl_SizeChanged"&gt; &lt;TabItem x:Name="tabItem1" Selector.Selected="tabParams_Selected"&gt; &lt;Grid&gt; &lt;ScrollViewer MaxHeight="1000"&gt; &lt;DynamicWpfcontrol/&gt; &lt;/ScrollViewer&gt; &lt;/Grid&gt; &lt;/TabItem&gt; &lt;TabItem x:Name="tabItem2" Selector.Selected="tabRepresentations_Selected"&gt; &lt;Grid&gt; &lt;WindowsFormsHost Margin="3"&gt; &lt;my:DynamicWinformControl AutoScroll="True" AutoSize="True" AutoSizeMode="GrowAndShrink"/&gt; &lt;/WindowsFormsHost&gt; &lt;/Grid&gt; &lt;/TabItem&gt; &lt;TabItem x:Name="tabItem3" Selector.Selected="tabAttributes_Selected"&gt; &lt;Grid&gt; &lt;WindowsFormsHost Margin="3"&gt; &lt;DynamicWinformControl AutoScroll="True" AutoSize="True" AutoSizeMode="GrowAndShrink"/&gt; &lt;/WindowsFormsHost&gt; &lt;/Grid&gt; &lt;/TabItem&gt; &lt;/TabControl&gt; &lt;/Grid&gt; </code></pre> <p></p> <pre><code>private Size tab1Size; private Size tab2Size; private Size tab3Size; private void tabControl_SizeChanged(object sender, SizeChangedEventArgs e) { if (tabItem1.IsSelected) tab1Size = e.NewSize; else if (tabItem2.IsSelected) tab2Size = e.NewSize; else if (tabItem1.IsSelected) tab3Size = e.NewSize; } </code></pre> <p>EDIT - I have updated the tabItemSelected events to use Measure and UpdateLayout instead of setting the width and height. I understand that measure sets the desired size. This is now working for remembering the width but the height is still being set back to the height of the contents.</p> <pre><code>private void tabItem1_Selected(object sender, RoutedEventArgs e) { if (tab1Size != Size.Empty &amp;&amp; !(tab1Size.Height == 0 &amp;&amp; tab1Size.Width == 0)) { tabControl.Measure(tab1Size); tabControl.Arrange(new Rect(tab1Size)); } this.SizeToContent = System.Windows.SizeToContent.WidthAndHeight; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload