tabitem的自动高度wpf

时间:2013-07-10 21:32:01

标签: c# wpf tabcontrol

我想通过带有2个标签的代码创建一个tabcontrol。我使用下面的代码:

                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);

但我的tabitem的高度是孩子控制的高度。这让我发疯了!

我试图在我的孩子控件中添加以下代码:

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

但是这让儿童控制太大了!

注意:我对手风琴也有同样的问题。

由于

2 个答案:

答案 0 :(得分:1)

删除您在XAML或后面的代码中设置的任何静态高度和宽度。 Horizo​​ntalContentAlignment和VerticalContentAlignment默认设置为Stretch。

答案 1 :(得分:0)

如果您的DockPrincipal是Dockpanel并且正在填写整个表单,您可以设置LastChildFill =“True”