TabControl / TabPanel / TabItem 的 WPF 样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3516841/
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-08 22:01:51  来源:igfitidea点击:

WPF Styles for TabControl / TabPanel / TabItem

wpfxamltabcontroltabpanel

提问by Jeroen

Here's a newbie question on the WPF TabControl, TabItem and TabPanel. There is a related question on StackOVF with an answer I happily used in my app. Here's a link to the answer, and the code snippet as well:

这是关于 WPF TabControl、TabItem 和 TabPanel 的新手问题。StackOVF 上有一个相关的问题,我在我的应用程序中很高兴使用了一个答案。这是答案的链接,以及代码片段:

WPF: Center TabItems in a TabControl

WPF:在 TabControl 中居中 TabItems

<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>

While this is wonderful, I'd love to move the Resources and Style stuff to a better location (a stylesheet or the like). My first attempt was to move the <TabControl.Resources>tag to the <Window.Resources>but this did not work. I tried several variations but couldn't get it to work. Here's an example of an attempt I somewhat expected to work:

虽然这很棒,但我很想将资源和样式内容移动到更好的位置(样式表或类似的位置)。我的第一次尝试是将<TabControl.Resources>标签移动到 ,<Window.Resources>但这不起作用。我尝试了几种变体,但无法使其正常工作。这是我有点希望工作的尝试示例:

<!-- Doesn't work as expected: -->
<Window.Resources>
    <Style TargetType="{x:Type TabPanel}">
        <Setter Property="HorizontalAlignment" Value="Center" />
    </Style>
</Window.Resources>

Searching the web and msdn didn't help me solve my problem, but instead left me with a second (related) question: what actually isa TabPanel, and how does it relate to the TabControl?

搜索网络和 msdn 并没有帮助我解决我的问题,而是给我留下了第二个(相关的)问题:TabPanel实际上什么,它与 TabControl 有什么关系?

Any help and tips would be much appreciated.

任何帮助和提示将不胜感激。

(Edited: commented in last example that the code doesn't work for me.)

(编辑:在最后一个例子中评论说该代码对我不起作用。)

回答by NVM

alt text

替代文字

TabControl uses a specialized TabPanel class and not a generic Panel like StackPanel because if you mess around with the TabControl you'll realize that the panel does quite a few things which generic panels dont. One is adjusting the tab header items in multiple rows. Another is that the the rows of items will be rearranged so that the selected tabitem header is always in the last row. I guess it might be doing even more

TabControl 使用专门的 TabPanel 类,而不是像 StackPanel 这样的通用面板,因为如果你弄乱了 TabControl,你会发现面板做了很多通用面板没有的事情。一种是在多行中调整选项卡标题项。另一个是项目的行将重新排列,以便选定的 tabitem 标题始终位于最后一行。我想它可能做得更多

I am quite interested in knowing why putting the style in the window resource section does not work. My initial reaction was it should work until I tried it. I am adding this as an answer because SO wont let me add an image in a comment.

我很想知道为什么将样式放在窗口资源部分不起作用。我最初的反应是在我尝试之前它应该可以工作。我将此添加为答案,因为 SO 不会让我在评论中添加图像。

回答by Chris Persichetti

You'll probably need to create a ControlTemplate to do this.

您可能需要创建一个 ControlTemplate 来执行此操作。

I'm not very familiar with ControlTemplates yet. I hacked this example from: http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.tabpanel.aspx

我对 ControlTemplates 还不是很熟悉。我从以下位置破解了这个例子:http: //msdn.microsoft.com/en-us/library/system.windows.controls.primitives.tabpanel.aspx

<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="0,0,4,-1" 
                                  IsItemsHost="True"
                                  KeyboardNavigation.TabIndex="1"
                                  HorizontalAlignment="Center"/>
                      </Grid>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>