使 WPF Tabcontrol 标题垂直堆叠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31360288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:15:48  来源:igfitidea点击:

Make WPF Tabcontrol Header Stack Vertically

c#wpfxaml

提问by James Okpe George

I have been trying to make my tabcontrol header stack up vertically, but no matter what I do it won't just work properly

我一直试图让我的 tabcontrol 标题垂直堆叠,但无论我做什么,它都不能正常工作

This is what I want:enter image description here

这就是我要的:在此处输入图片说明

Its funny because this display is as a result of some error from Visual Studio Editor.

这很有趣,因为这个显示是由于 Visual Studio 编辑器的一些错误造成的。

But this is what I get

但这就是我得到的

enter image description here

在此处输入图片说明

I Tried this code

我试过这个代码

<DockPanel>
    <Menu DockPanel.Dock="Top">
        <MenuItem Header="File">
            <MenuItem Header="Prepare Report"></MenuItem>
            <MenuItem Header="Print PDF"></MenuItem>
            <Separator/>
            <MenuItem Header="Exit"></MenuItem>
        </MenuItem>
        <MenuItem Header="Configurations">
            <MenuItem Header="Pay Grade"></MenuItem>
            <MenuItem Header="Staff Levels"></MenuItem>
            <MenuItem Header="Departments"></MenuItem>
            <MenuItem Header="Authroizations"></MenuItem>
        </MenuItem>
        <MenuItem Header="Help">
            <MenuItem Header="Help Content"/>
            <Separator/>
            <MenuItem Header="About Software"/>
        </MenuItem>
    </Menu>
    <StackPanel Orientation="Horizontal"></StackPanel>
    <TabControl TabStripPlacement="Left" DockPanel.Dock="Top" Style="{DynamicResource NavigationMenu}" Margin="20 0">
        <TabControl.Resources>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <ContentPresenter Content="{TemplateBinding Content}">
                                <ContentPresenter.LayoutTransform>
                                    <RotateTransform Angle="-360" />
                                </ContentPresenter.LayoutTransform>
                            </ContentPresenter>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.Resources>
        <TabItem x:Name="Dashboard">
            <TabItem.Header><TextBlock Text="DASHBOARD" FontSize="20" ></TextBlock></TabItem.Header>
        </TabItem>
        <TabItem  x:Name="Staffs">
            <TabItem.Header>
                <TextBlock Text="STAFFS" ></TextBlock>
            </TabItem.Header>
        </TabItem>
        <TabItem  x:Name="Departments">
            <TabItem.Header>
                <TextBlock Text="DEPARTMENTS" ></TextBlock>
            </TabItem.Header>
        </TabItem>
        <TabItem x:Name="Settings">
            <TabItem.Header>
                <TextBlock Text="SETTINGS" ></TextBlock>
            </TabItem.Header>
        </TabItem>
    </TabControl>
</DockPanel>

But it breaks my UI and displays like this

但它打破了我的用户界面并像这样显示

Dark Shade AddedThe Dark part is not supposed to be there, I do not even know where it is coming from

添加了深色阴影黑暗部分不应该在那里,我什至不知道它来自哪里

回答by olitee

You should be able to solve this by removing your data template / style, and simply by setting the TabStripPlacementproperty of the TabControlto Left

您应该能够通过删除数据模板/样式来解决此问题,只需将TabStripPlacement属性设置TabControlLeft

Or via a style setter, something like this:

或者通过样式设置器,如下所示:

<Style TargetType="TabControl">
    <Setter Property="TabStripPlacement" Value="Left"></Setter>
</Style>

The defaultTabStrip (actually a TabPanel) is simply a StackPanel within a template. The TabStripPlacement changes the location of the TabStrip within the TabControl - but also switches the StackPanel from Horizontal to Vertical orientation ... which should give you result you're looking for.

默认TabStrip控件(实际上是一个的TabPanel)只是一个模板内的StackPanel。TabStripPlacement 改变了 TabStrip 在 TabControl 中的位置 - 但也将 StackPanel 从水平方向切换到垂直方向......这应该会给你你正在寻找的结果。

回答by gomi42

I hope the following gives you an idea of how your requirements can be accomplished. The basic idea is to switch from TabPanel to a StackPanel in the TabControl style, don't any Background colors and rotate the header in the TabItem style.

我希望以下内容能让您了解如何实现您的要求。基本思路是从TabPanel切换到TabControl风格的StackPanel,不加背景色,旋转TabItem风格的header。

<Window x:Class="ShowScroller.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Width="525"
        Height="421">
    <Window.Resources>
        <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.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                                                <EasingColorKeyFrame KeyTime="0" Value="#FFAAAAAA" />
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <ContentPresenter x:Name="PART_SelectedContentHost"
                                              Grid.Column="1"
                                              Margin="0"
                                              ContentSource="SelectedContent" />
                            <StackPanel x:Name="HeaderPanel"
                                        Grid.Row="0"
                                        Margin="0,0,4,-1"
                                        Panel.ZIndex="1"
                                        Background="Transparent"
                                        IsItemsHost="True"
                                        KeyboardNavigation.TabIndex="1" />
                            <Border x:Name="Border"
                                    Grid.Row="1"
                                    BorderThickness="1"
                                    CornerRadius="2"
                                    KeyboardNavigation.DirectionalNavigation="Contained"
                                    KeyboardNavigation.TabIndex="2"
                                    KeyboardNavigation.TabNavigation="Local" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid x:Name="Root">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselected" />
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                                                <EasingColorKeyFrame KeyTime="0" Value="#FF0000FF" />
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal" />
                                    <VisualState x:Name="MouseOver" />
                                    <VisualState x:Name="Disabled" />
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="Border"
                                    Margin="0,0,0,0"
                                    BorderBrush="Gainsboro"
                                    BorderThickness="0,0,2,0" />
                            <TextBlock Margin="12,10,12,10" Text="{TemplateBinding Header}">
                                <TextBlock.LayoutTransform>
                                    <TransformGroup>
                                        <ScaleTransform />
                                        <SkewTransform />
                                        <RotateTransform Angle="-90" />
                                        <TranslateTransform />
                                    </TransformGroup>
                                </TextBlock.LayoutTransform>
                            </TextBlock>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <TabControl Width="391"
                    Height="324"
                    Margin="49,43,0,0"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Top">
            <TabItem FontSize="20"
                     Header="TabItem"
                     RenderTransformOrigin="0.5,0.5">
                <Grid Background="#FFF5E5E5">
                    <TextBlock Margin="55,107,0,0"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Top"
                               Text="TextBlock 1"
                               TextWrapping="Wrap" />
                </Grid>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5F5">
                    <TextBlock Margin="54,98,0,0"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Top"
                               Text="TextBlock 2"
                               TextWrapping="Wrap" />
                </Grid>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFEBF5E5">
                    <TextBlock Margin="54,98,0,0"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Top"
                               Text="TextBlock 3"
                               TextWrapping="Wrap" />
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</Window>