How to Stretch WPF Tab Item Headers to Parent Control Width

后端 未结 11 1960
花落未央
花落未央 2020-11-29 23:17

Is there a way in XAML to cause the tab item headers to stretch across the width of the tab control?

For example, I have three tabs: red, blue and green. If I have a

11条回答
  •  鱼传尺愫
    2020-11-29 23:33

    I took Jordan's example and made some changes to it. This version should work for any number of tabs:

    namespace WpfApplication1.Converters
    {
        public class TabSizeConverter : IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType, object parameter,
                System.Globalization.CultureInfo culture)
            {
                TabControl tabControl = values[0] as TabControl;
                double width = tabControl.ActualWidth / tabControl.Items.Count;
                //Subtract 1, otherwise we could overflow to two rows.
                return (width <= 1) ? 0 : (width - 1);
            }
    
            public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
                System.Globalization.CultureInfo culture)
            {
                throw new NotSupportedException();
            }
        }
    }
    

    Same namespace in the xaml:

    xmlns:local="clr-namespace:WpfApplication1.Converters"
    

    And this will make all tabs use it:

    
        
        
    
    

提交回复
热议问题