WPF:如何在使用数据绑定时将 controlTemplate 应用于 TabControl 的 tabItem 标头?

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

WPF: How to apply a controlTemplate to a TabControl's tabItem Header while using Databinding?

wpfxamldata-bindingcontroltemplatetabitem

提问by WinW

I have a TabControl which is acquiring tabs and content through DataBinding. Instead of normal Tabs, I wanted Tabs to be of custom shape (say, Ellipse) for all tabs, So I have a ControlTemplate which i want to apply for all tabs.Being new to WPF,I am just confused on how to apply a controlTemplate to a TabControl's tabItem header while using Databinding?

我有一个 TabControl,它通过 DataBinding 获取选项卡和内容。我希望所有选项卡的选项卡都具有自定义形状(例如椭圆),而不是普通选项卡,所以我有一个 ControlTemplate,我想将其应用于所有选项卡。作为 WPF 的新手,我只是对如何应用一个使用数据绑定时将 controlTemplate 转换为 TabControl 的 tabItem 标头?

<Window.Resources>
    <ControlTemplate x:Key="TabItemControlTemplate1" TargetType="{x:Type TabItem}">
        <Grid SnapsToDevicePixels="True">
            <Border CornerRadius="12" x:Name="Bd" BorderBrush="Red" BorderThickness="1" Padding="{TemplateBinding Padding}">
               <ContentPresenter x:Name="Content" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/>
            </Border>
        </Grid>

    </ControlTemplate>
</Window.Resources>

<Grid x:Name="LayoutRoot">
     <TabControl
    ItemsSource="{Binding CarCollection}">
                <TabControl.ItemTemplate>
                    <!-- this is the header template-->
                    <DataTemplate>
                        <TextBlock
                Text="{Binding DisplayName}" />
                    </DataTemplate>
                </TabControl.ItemTemplate>
                <TabControl.ContentTemplate>
                    <!-- this is the body of the TabItem template-->
                    <DataTemplate>
                        <ContentControl
                Content="{Binding Value}" />
                    </DataTemplate>
                </TabControl.ContentTemplate>
            </TabControl>
</Grid>

In those cases where databinding is not used, I know how to apply it. Issue is when Databinding is used, I am able to achieve this through the below code.

在不使用数据绑定的情况下,我知道如何应用它。问题是当使用数据绑定时,我可以通过以下代码实现这一点。

<TabControl HorizontalAlignment="Left" Height="224" Margin="72,66,0,0" VerticalAlignment="Top" Width="412">
        <TabItem Header="TabItem" Template="{DynamicResource TabItemControlTemplate1}" Height="19.96" VerticalAlignment="Top">
            <Grid Background="#FFE5E5E5"/>
        </TabItem>
        <TabItem Header="TabItem">
            <Grid Background="#FFE5E5E5"/>
        </TabItem>
    </TabControl>

Any help on this?

有什么帮助吗?

采纳答案by har07

You can try using Styleto set Templateof TabItemgenerated from binding :

您可以尝试使用Style到一套TemplateTabItem自结合产生的:

<TabControl ItemsSource="{Binding CarCollection}">
    <TabControl.Resources>
        <Style TargetType="TabItem">
            <Setter Property="Template" Value="{DynamicResource TabItemControlTemplate1}"/>
        </Style>
    </TabControl.Resources>
    <TabControl.ItemTemplate>
        <!-- this is the header template-->
        <DataTemplate>
            <TextBlock Text="{Binding DisplayName}" />
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <!-- this is the body of the TabItem template-->
        <DataTemplate>
            <ContentControl Content="{Binding Value}" />
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>