9

I have a TabControl that is bound to a collection of Viewmodels, which get translated into an appropriate to be drawn into the tab:

<Window.Resources>
    <ResourceDictionary>
        <DataTemplate DataType="{x:Type charting:LineFormatViewModel}">
            <charting:LineFormatView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type charting:IndexSettingsViewModel}">
            <charting:IndexSettingsView />
        </DataTemplate>
        ....
    </ResourceDictionary>
</Window.Resources>

<TabControl ItemSource="Binding ViewModels" />

I've been trying to find a way to always draw the TabControl at the maximum width and height of any of it's children. Let WPF Tabcontrol height assume height of largest item? mentions a couple of ways to achieve this in the answers (from what I udnerstood):

  • using Grid with SharedSizeGroup - seems to need to be applied to a DataTemplate on the actual content of the TabControl, which overwrites the automated View drawing achieveing by the VM->View mapping created in the resourcedictionary
  • Using a width/height converter, which would require my TabControl to be bound to a collection of UI elements instead of viewmodels

Does anyone have any experience solving a similar issue? I seem to be hitting more walls every time I make some progress on this.

Community
  • 1
  • 1
Zepee
  • 1,640
  • 3
  • 20
  • 40
  • [This answer](http://stackoverflow.com/a/14865928/302677) may be on the right track. I think you'll need to use the ItemsContainerGenerator to generate the UI item for each element in the ItemsSource, and determine the size of that item (perhaps using code from [this answer](http://stackoverflow.com/a/1074344/302677)?) to figure out what the Maximum size would be. – Rachel Jul 07 '15 at 14:31
  • @zepee, were you able to find the solution to it? – Jayakrishnan Feb 04 '19 at 06:07

2 Answers2

0

You need to use named hidden controls that can fit automatically and create straight bind to their width/height like:

 Width="{Binding ActualWidth, ElementName=MainGrid}"
MarmiK
  • 5,639
  • 6
  • 40
  • 49
0

In case anyone else is having this problem, I solved it with the following code in the window's ContentRendered event handler:

tabControl.SelectedIndex = 1;
UpdateLayout();
SizeToContent = SizeToContent.Manual;
tabControl.SelectedIndex = 0;

tabsControl is the name of the TabControl object. Essentially what we do is switch to the tab with the greatest height (at index 1 in my example), update the window's layout, tell the window to stop resizing to fit its contents, and then switch back to the first tab. Of course, this solution assumes you know which tab has the greatest height and that you won't need the window to resize to its contents after the initial render.

Razz
  • 35
  • 3