样式TabItems,不会影响嵌套的TabControls

时间:2016-09-15 23:50:31

标签: wpf xaml

我是TabControl的TabItem样式。问题是,样式会影响嵌套TabControls的项目。传播样式是我知道进入TabItems的唯一方式。任何人都知道如何在外部 TabControl上设置TabItems样式?

在我的情况下,内部标签是在插件中定义的,因此我无法访问它们以尝试this answer

演示应用

这是我的情况的演示应用程序。

Red style affects inner tabs

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TabControl ItemsSource="{Binding}">
            <TabControl.Resources>
                <Style TargetType="TabItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="TabItem">
                                <Border Name="Border" Background="Red">
                                    <ContentPresenter ContentSource="Header"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TabControl.Resources>
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding TabName}" />
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <ContentControl Content="{Binding TabConent}" />
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public class TabData
        {
            public string TabName { get; set; }
            public Label TabConent
            {
                get
                {
                    // In real case, this TabControl from someone else's plugin
                    var content = new TabControl();
                    content.Items.Add(new TabItem() { Header = "Nested Tab Item" });
                    return new Label() { Content = content };
                }
            }
        }

        public MainWindow()
        {
            DataContext = new ObservableCollection<TabData>() { new TabData() { TabName = "Tab Item" } }; ;
            InitializeComponent();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用此功能,并告诉您这是否符合要求:

<TabControl.Resources>
    <Style TargetType="TabItem">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TabControl, AncestorLevel=2}}" Value="{x:Null}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TabItem">
                            <Border Name="Border" Background="Red">
                                <ContentPresenter ContentSource="Header"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>                    
    </Style>
</TabControl.Resources>