TabControl/TabPanel/TabItem 的 WPF 样式 [英] WPF Styles for TabControl / TabPanel / TabItem

查看:108
本文介绍了TabControl/TabPanel/TabItem 的 WPF 样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 中居中 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:

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

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

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

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

Any help and tips would be much appreciated.

(在最后一个例子中评论说代码对我不起作用.)

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

推荐答案

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

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

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

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.

这篇关于TabControl/TabPanel/TabItem 的 WPF 样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆