You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改TabControl控件的背景颜色?

TabControl 外部区域颜色修改方案

针对 WinForm 框架

默认WinForm的TabControl由系统接管绘制,直接设置BackColor不会作用到标签栏周围的空白区域,需要开启自绘模式:

  1. 先为TabControl设置两个基础属性:
    • DrawMode = TabDrawMode.OwnerDrawFixed
    • 可提前设置BackColor为你需要的目标背景色
  2. 绑定TabControl的DrawItem事件,事件处理逻辑参考如下:
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
    var tabCtrl = sender as TabControl;
    // 绘制当前标签头
    Brush tabBackBrush = e.Index == tabCtrl.SelectedIndex ? Brushes.DarkGray : Brushes.Gray;
    e.Graphics.FillRectangle(tabBackBrush, e.Bounds);
    TextRenderer.DrawText(e.Graphics, tabCtrl.TabPages[e.Index].Text, e.Font, e.Bounds, Color.White);
    
    // 绘制标签栏外围空白区域
    Rectangle totalBounds = tabCtrl.ClientRectangle;
    Rectangle pageBounds = tabCtrl.DisplayRectangle;
    Region blankRegion = new Region(totalBounds);
    blankRegion.Exclude(pageBounds);
    // 此处Brush替换为你需要的空白区域颜色
    e.Graphics.FillRegion(Brushes.Black, blankRegion);
}

针对 WPF 框架

WPF默认TabControl的控件模板没有绑定Background属性,所以直接设置Background不会生效,需要重写控件模板:

<TabControl Background="Black"> <!-- 此处设置你需要的整体背景色 -->
    <TabControl.Template>
        <ControlTemplate TargetType="TabControl">
            <Grid Background="{TemplateBinding Background}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <TabPanel Grid.Row="0" IsItemsHost="True"/>
                <ContentPresenter Grid.Row="1" ContentSource="SelectedContent" Margin="0,5,0,0"/>
            </Grid>
        </ControlTemplate>
    </TabControl.Template>
    <!-- 此处添加你的TabItem内容 -->
</TabControl>

重写模板后,直接修改TabControl的Background属性就可以同步修改外部空白区域的颜色。

内容的提问来源于stack exchange,提问作者VWWE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 00:45:04