如何移动TabControl的TabItem?

时间:2012-04-12 06:26:44

标签: wpf tabcontrol

<Border Background="#FF260F54">
    <TabControl Name="MyTabCtrl" SelectionChanged="MyTabCtrl_SelectionChanged" >
        <TabItem Name="TItem01" Header="01">
            <TextBlock>TItem01</TextBlock>
        </TabItem>
        <TabItem Name="TItem02" Header="02">
            <TextBlock>TItem02</TextBlock>
        </TabItem>
    </TabControl>
</Border>

我想让TItem01向左移动200像素,然后显示TItem02。 我该怎么办?请帮我。非常感谢你!

1 个答案:

答案 0 :(得分:1)

不知道这是否是最简单的方法,但我会修改默认的TabControl样式:

<SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
<SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
<SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" />
<Style  TargetType="{x:Type TabControl}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <Grid KeyboardNavigation.TabNavigation="Local">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <TabPanel 
                        Name="HeaderPanel"
                        Grid.Row="0"
                        Panel.ZIndex="1" 
                        Margin="200,0,4,-1" 
                        IsItemsHost="True"
                        KeyboardNavigation.TabIndex="1"
                        Background="Transparent" />
                    <Border 
                        Name="Border" 
                        Grid.Row="1" 
                        Background="{StaticResource WindowBackgroundBrush}" 
                        BorderBrush="{StaticResource SolidBorderBrush}" 
                        BorderThickness="1" 
                        CornerRadius="2" 
                        KeyboardNavigation.TabNavigation="Local"
                        KeyboardNavigation.DirectionalNavigation="Contained"
                        KeyboardNavigation.TabIndex="2" >
                        <ContentPresenter 
                            Name="PART_SelectedContentHost"
                            Margin="4"
                            ContentSource="SelectedContent" />
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

请注意TabPanelContolTemplate元素的Margin属性?该边距决定了标签的开始位置。默认情况下,它是0,0,4,-1,我将其修改为200,0,4,-1以符合您的要求。

如果您想知道我是如何制作这种风格的,有几种方法。最简单的方法是使用Expression Blend。它有一个“打破”你的控件并公开它的默认部件和样式的选项。另一种方法是只搜索MSDN的ControlTemplates,因为它们是公共的。 (我使用了第二种方法)

相关问题