Reputation: 35627
I have a WPF TabControl, containing two TabItems with different width and height. The weird thing is when I switch between the items, the size of the TabControl is resized to closely fit the selected tab items. For example, if I click the smaller TabITem, the TabControl becomes smaller and if I select the bigger one, it becomes bigger.
I think this would look silly from user point of view, and this is not the case in normal tab GUI such as Windows properties. Is it possible to set the width and height to fit the largest item? I want to try avoid having to set a constant value
Upvotes: 2
Views: 3689
Reputation: 1368
I had the same problem. Found out it was caused by the Parent Control. If you put it in a StackPanel
, the Width and Height depends on the ActualWidth and -Height of the content.
I had it placed in a Grid
, but the Width of ColumnDefinition
was set to "Auto". When I changed it to "*" the Width of the TabControl didn't change anymore.
Upvotes: 0
Reputation: 4264
You should better have a fixed TabControl size relative to the size of window and let the TabItems resized according to the size of TabControl.
EDIT:
Make sure that the row or column that contains the TabControl
is not auto sized. If your code looks like following, your TabControl
will be resized according to the active TabItem
.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=".20*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height=".20*"/>
</Grid.RowDefinitions>
<TabControl Grid.Row="1" BorderBrush="Red" BorderThickness="1">
<TabItem Header="Blue">
<Rectangle Width="200" Height="200" Fill="Blue"></Rectangle>
</TabItem>
<TabItem Header="Black">
<Rectangle Width="100" Height="100" Fill="Black"></Rectangle>
</TabItem>
</TabControl>
</Grid>
And if you change the line <RowDefinition Height="Auto"/>
in the above code to <RowDefinition Height="*"/>
it'll solve the issue.
You can also share the code and we can have a look at it.
Upvotes: 3
Reputation: 6269
Well if you don't want to have a fixed size, I see another option but this one is not cheap in terms of performance. What you need to know is the acutal width and height the second tab would need. This means you can bind the height and width of the TabControl to the DesiredSize of the bigger item plus some additional space. But as the second item might not be measured already (check IsMeasureValid property) then you need to call UpdateLayout which does the whole layouting for the controls.
But in general I really wouldn't recommend you to go that path, as playing with the layouting is usually pain in the a.... . I know that's not what you are asking for, but from experience -> don't do it.
Why really not giving a fixed size and add scrollviewers? Or us a dockpanel and dock everything somewhere, and give the tabcontrol the remaining space, so that it will always fill up your window.
hth Martin
Upvotes: 0