代码之家  ›  专栏  ›  技术社区  ›  Inferis

让WPF Tabcontrol height假设最大项目的高度?

  •  12
  • Inferis  · 技术社区  · 17 年前

    有没有办法让tabcontrol获取最大选项卡项的大小(实际上是tabitem的内容)?

    由于tabcontrol没有指定特定的大小,它应该自动调整大小:它可以正确地调整大小,但当您切换选项卡时,它会自动调整自身大小,以适应当前选定选项卡内容的高度(和宽度)。

    我不希望调整大小,让tabcontrol假设最大项目的高度,但仍然让它自动调整大小。

    Height ActualHeight 和 Items 选项卡控件的属性。但可惜的是 内容元素的值始终为0。

            <TabItem Header="Core" > 
                <Grid Margin="5">
                    <Grid.Height>
                        <MultiBinding Converter="{Converters1:AllTabContentEqualHeightConverter}">
                            <Binding Path="ActualHeight" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}"/>
                            <Binding Path="Items" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}"/>
                        </MultiBinding>
                    </Grid.Height>
    
                ...
    

    5 回复  |  直到 17 年前
        1
  •  26
  •   Robert Harvey    8 年前

    是的,可以这样做: reuse-grid-rowdefinitions-for-each-tabitem

        <TabControl Grid.IsSharedSizeScope="True">
            <TabItem Header="Tab 1">
                <Grid >
                    <Grid.RowDefinitions>
                        <RowDefinition SharedSizeGroup="xxx"/>
                    </Grid.RowDefinitions>
                </Grid>
            </TabItem>
            <TabItem Header="Tab 2">
                <Grid >
                    <Grid.RowDefinitions>
                        <RowDefinition SharedSizeGroup="xxx"/>
                    </Grid.RowDefinitions>
                </Grid>
            </TabItem>
       </TabControl>
    
        2
  •  7
  •   Tim Cooper    15 年前

    问题是 TabControl 从不

    This blog post Eric Burke写的应该让你开始。从我浏览他的文章可以看出,你需要修改它,以便:

    • 选项卡控件 已加载。
        3
  •  5
  •   Inferis    17 年前

    事实上,我认为这更容易解决。 因为我有一个用于 TabControl 不管怎样,我设置了 ContentPresenter 选项卡控件 ,如有必要,对其进行测量(使用 Measure )和支票 DesiredSize 适合我需要的尺码。

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var items = value as ItemCollection;
    
            if (items == null)
                return null;
    
            double max = 0;
            foreach (TabItem item in items)
            {
                var content = item.Content as FrameworkElement;
                if (content == null) continue;
    
                if (!content.IsMeasureValid)
                    content.Measure(new Size(int.MaxValue, int.MaxValue));
    
                var height = content.DesiredSize.Height;
                if (max < height)
                    max = height;
            }
    
            return max;
        }
    

    • 每个选项卡内容都应该是一个 FrameworkElement
    • 内容在加载后不会更改大小(因为只有在Items属性更改时才会调用转换器,即只调用一次)。
        4
  •  2
  •   Libor    9 年前

    Grid.IsSharedSizeScope 方法如上所示。

    SetCurrentValue 使用,而不仅仅是设置 SelectedIndex

    private void TabControl_OnLoaded(object sender, RoutedEventArgs e)
    {
        //NOTE: loop through tab items to force measurement and size the tab control to the largest tab
        TabControl tabControl = (TabControl)sender;
    
        // backup selection
        int indexItemLast = tabControl.SelectedIndex;
    
        int itemCount = tabControl.Items.Count;
    
        for (
            int indexItem = (itemCount - 1);
            indexItem >= 0;
            indexItem--)
        {
            tabControl.SetCurrentValue(Selector.SelectedIndexProperty, indexItem);
            tabControl.UpdateLayout();
        }
    
        // restore selection
        tabControl.SetCurrentValue(Selector.SelectedIndexProperty, indexItemLast);
    }
    
        5
  •  1
  •   Tiberiu Ana    17 年前

    它可能不是以正确的WPF方式,但是,如果您已经拥有了所有的内容元素,那么您可以在加载时循环它们并设置元素的高度 TabControl 在程序上。