0
<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。我该怎么办?请帮我。非常感谢你!

4

1 回答 1

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>

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

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

于 2012-04-12T13:44:09.323 回答