Let WPF Tabcontrol height assume height of largest item?

前端 未结 5 857
余生分开走
余生分开走 2020-12-09 04:46

Is there any way to have to tabcontrol take the size of the largest tab item (well, actually, the tabitem\'s content)?

Since the tabcontrol has no specific size assi

5条回答
  •  Happy的楠姐
    2020-12-09 05:36

    This worked for me in conjunction with Grid.IsSharedSizeScope approach shown above.

    Note that SetCurrentValue is used instead of just setting the SelectedIndex property - this way we keep possible existing bindings:

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

提交回复
热议问题