WPF: Center TabItems in a TabControl

不打扰是莪最后的温柔 提交于 2019-11-29 17:37:55

问题


in my XAML code, I have a TabControl with multiple items. The problem I have is that I can not center the tabitems about the content area. The tabs are always starting on the left side, but I need them centered. This is my code:

<TabControl>
    <TabItem Header="Test 1"  Style="{StaticResource LeftTab}" Height="40" />
    <TabItem Header="Test 2"  Style="{StaticResource MiddleTab}"  />
    <TabItem Header="Test 3"  Style="{StaticResource MiddleTab}" />
    <TabItem Header="Test 4" Style="{StaticResource RightTab}"  />
</TabControl>

I do not know a property to center the items - any idea?


回答1:


Internally, the TabControl uses a TabPanel to layout the tabs. Using the default template, you just need to set the HorizontalAlignment of the TabPanel through a style:

<TabControl>
    <TabControl.Resources>
        <Style TargetType="{x:Type TabPanel}">
            <Setter Property="HorizontalAlignment" Value="Center" />
        </Style>
    </TabControl.Resources>

    <TabItem Header="Test 1" />
    <TabItem Header="Test 2" />
    <TabItem Header="Test 3" />
    <TabItem Header="Test 4" />
</TabControl>


来源:https://stackoverflow.com/questions/2273567/wpf-center-tabitems-in-a-tabcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!