如何在WPF TabControl中更改TabItems的重叠顺序

时间:2011-01-14 12:35:10

标签: .net wpf tabcontrol

我用Path对象创建了垂直TabItems。选定的TabItem与未选择的TabItem重叠,这很好。通过在TabItem模板中设置负边距来完成重叠。

对于未选择的TabItems,TabItem现在与下面的TabItem重叠。例如,在图片中,选项卡4与选项卡3重叠,选项卡3与选项卡2重叠。

我想更改未选中的Tab项的重叠顺序,以便未选中的TabItem与下面的TabItem重叠,并与上面的TabItem重叠,例如选项卡2与选项卡3重叠,选项卡3与选项卡4重叠。

我试图设置TabPanel的FlowDirection属性,但这不起作用。

我怎样才能做到这一点?任何帮助表示赞赏。提前谢谢!

未选中的TabItems错误重叠:

Vertical TabItems

XAML的代码:

<Style x:Key="styleMainNavTabControl" TargetType="{x:Type TabControl}">
    <Setter Property="TabStripPlacement" Value="Left" />
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="200"/>
                    </Grid.ColumnDefinitions>
                    <Border Grid.Column="0" Background="White" BorderBrush="Black" BorderThickness="0,0,1,0" Padding="20">
                        <ContentPresenter ContentSource="SelectedContent" />
                    </Border>
                    <Border Grid.Column="1" Padding="0,30,10,0" Background="#F7F3F7">
                        <TabPanel Panel.ZIndex="1" Margin="-1,0,0,0" FlowDirection="RightToLeft" IsItemsHost="True" Background="Transparent"/>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="styleMainNavTabItem" TargetType="{x:Type TabItem}">
    <Setter Property="MinHeight" Value="90" />
    <Setter Property="FontSize" Value="14" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <Grid Margin="0,0,0,-35">
                    <Path Name="TabPath" Stroke="Black" StrokeThickness="1" Fill="LightGray" Data="M 0,0 a 10,10 0 0 0 10,10 h 150 a 20,20 0 0 1 20,20 v 60 a 20,20 0 0 1 -20,20 h -150 a 10,10 0 0 0 -10,10 z" />
                    <ContentPresenter ContentSource="Header" Margin="10,2,10,2" VerticalAlignment="Center" TextElement.Foreground="#FF000000"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Panel.ZIndex" Value="100" />
                        <Setter TargetName="TabPath" Property="Fill" Value="White" />
                        <Setter TargetName="TabPath" Property="Data" Value="M 0,0 a 10,10 0 0 0 10,10 h 150 a 20,20 0 0 1 20,20 v 60 a 20,20 0 0 1 -20,20 h -150 a 10,10 0 0 0 -10,10" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="False">
                        <Setter Property="Panel.ZIndex" Value="90" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

1 个答案:

答案 0 :(得分:3)

我认为最简单的方法是使用使用Parent ItemsContainerGenerator来确定ZIndex的MultiBinding / Converter。它看起来像这样:

public class TabZIndexConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var tabItem = values[0] as TabItem;
        var tabControl = values[1] as TabControl;
        if (tabItem == null || tabControl == null) return Binding.DoNothing;

        var count = (int)values[2];

        var index = tabControl.ItemContainerGenerator.IndexFromContainer(tabItem);

        return count - index;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

然后,您将更改TabItem样式以在setter中设置ZIndex(并删除第二个触发器,因为它将禁用此setter):

<Style x:Key="styleMainNavTabItem" TargetType="{x:Type TabItem}">
    <Setter Property="Panel.ZIndex">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource tabZIndexConverter}">
                <Binding RelativeSource="{RelativeSource Self}" />
                <Binding RelativeSource="{RelativeSource AncestorType={x:Type TabControl}}" />
                <Binding Path="Items.Count" RelativeSource="{RelativeSource AncestorType={x:Type TabControl}}" />
            </MultiBinding>
        </Setter.Value>
    </Setter>
    ...
</Style>