【问题标题】:TabItem - how to change template and leave default style?TabItem - 如何更改模板并保留默认样式?
【发布时间】:2016-12-21 04:42:33
【问题描述】:

我有以下代码:

<Window x:Class="kkk.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="tabitemstyle" TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid>
                            <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                                <ContentPresenter x:Name="ContentSite" ContentSource="Header"></ContentPresenter>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>

        <TabControl>
            <TabItem Header="tab1" Style="{StaticResource tabitemstyle}"></TabItem>
            <TabItem Header="tab1" Style="{StaticResource tabitemstyle}"></TabItem>
        </TabControl>

    </Grid>
</Window>

我想保留 TabItem 的默认样式 - 我的意思是 padding/margins/BorderBrush/BorderThickness 等等...这就是我写BasedOn="..." 的原因。 但它不起作用 - 我认为它会在没有任何自定义样式的情况下呈现与 TabItem 相同的效果,但事实并非如此 - 它只是呈现一些文本(通过 ContentPresenter)。该样式没有获得默认属性值......我该怎么做? 我需要 ControlTemplate 在我的风格中...

【问题讨论】:

    标签: wpf tabcontrol tabitem


    【解决方案1】:

    您正在覆盖 TabItem.Template,它告诉 WPF 如何绘制对象

    如果您想创建一个基于默认 TabItem.Template 的模板,您可以从 MSDN 获取 default template 并将其更改为您想要的任何内容。

    【讨论】:

    • 这些子页面仍然不提供默认值,而是提供示例。我更喜欢链接到this question
    • @H.B.谢谢,我将尝试开始使用默认模板问题的答案而不是 MSDN 示例。
    • 我同意 H.B. MSDN 不提供原始模板,但提供一些示例...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 2020-05-21
    • 2023-03-30
    • 2011-12-05
    • 2017-05-02
    相关资源
    最近更新 更多