user2088807
user2088807

Reputation: 1408

Auto Height of tabitem wpf

I'd like to create a tabcontrol through code with 2 tabs . I used the code below :

                TabControl tb = new TabControl();
                tb.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                tb.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                tb.VerticalContentAlignment = System.Windows.VerticalAlignment.Stretch;
                tb.HorizontalContentAlignment =  System.Windows.HorizontalAlignment.Stretch;
                ressource_design.initialiserTabControl(tb);
                tb.Margin = new Thickness(10, 10, 10, 10);
                TabItem ti1 = new TabItem();
                ti1.Header = ServicesLangue.RM.GetString("CONTENU_ACCUEIL_LISTE_SAS");
                ti1.Content = _listeSAS;
                tb.Items.Add(ti1);

                TabItem ti2 = new TabItem();
                ti2.Header = ServicesLangue.RM.GetString("CONTENU_ACCUEIL_TBSM");
                ti2.Content = _tbsm;
                tb.Items.Add(ti2);

                this.DockPrincipal.Children.Add(tb);

But the height of my tabitem is the height of the children control. This is driving me crazy !

I've tried to add the code below in my child control:

Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=ActualWidth}"
                Height="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Grid}}, Path=ActualHeight}"

But this makes le child control too big !

Note: I have the same problem with the Accordion.

Thanks

Upvotes: 0

Views: 4903

Answers (2)

Nitesh
Nitesh

Reputation: 7409

Remove any static Height and Width you set in XAML or Code behind. HorizontalContentAlignment and VerticalContentAlignment are by default set to Stretch.

Upvotes: 3

Jonah Kunz
Jonah Kunz

Reputation: 670

If your DockPrincipal is a Dockpanel and is filling your whole form, you could set LastChildFill="True"

Upvotes: 0

Related Questions