4
votes

The following scenario bothers me: I have a simple WPF Window that has a TabControl as content. The ItemsSource of the TabControl is bound to a list of objects. In order to visualize the objects I defined some DataTemplates. As the list of objects may have different types, the right visualization is chosen by the default template selector. This works fine and does not cause any trouble.

The issue that came up is the size of the window. The DataTemplates have different sizes. And I want the dialog to have a size that the largest DataTemplate fits. When I use SizeToContent in the Window, the Window changes its size everytime I change the tabs.

So, my questions is, how can I achieve to make the window fit the largest TabItem (which size is determined by the DataTemplate)?

thanks, Florian

1

1 Answers

4
votes

The problem you are having, is that because the larger DataTemplate is not shown, it's size is not taken into account when sizing to content.

Your options:
1) Manually set (min)width/height on appropriate controls (tabcontrol, Window, DataTemplate, etc)
2) If you know that a certain tab is always going to be bigger than the rest, you can bind the width/height of the other tabs to the bigger tab:

<TabItem>
    <StackPanel Name="stackPanelBiggest" />
</TabItem>
<TabItem>
    <StackPanel Width="{Binding ElementName=stackPanelBiggest, Path=ActualWidth}" />
</TabItem>

I think that for the above to work, the biggest tab has to be first one shown. Although the tab control destroys the visual tree of the previous tab when swithching to another tab, this method is still working for me (despite the fact that the ActualWidth should be 0 or NaN, after switching tabs).