C# WPF XAML:指向选项卡项的自定义样式

C# WPF XAML:指向选项卡项的自定义样式,c#,wpf,xaml,tabs,resourcedictionary,C#,Wpf,Xaml,Tabs,Resourcedictionary,我有一个名为TabControlStyles.xaml的文件,其中有两种可用的样式,第一种是标准的TabControl样式,第二种是SearchResultsTab。代码如下: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

我有一个名为TabControlStyles.xaml的文件,其中有两种可用的样式,第一种是标准的TabControl样式,第二种是SearchResultsTab。代码如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
                xmlns:Actions="clr-namespace:Selen.Wpf.SystemStyles.Actions">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/Selen.Wpf.Core;component/Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type TabControl}" x:Key="StandardTabControl">
    <Style.Resources>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid Background="{TemplateBinding Background}" SnapsToDevicePixels="true" Height="23">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="25"/>
                            </Grid.ColumnDefinitions>
                            <ContentPresenter ContentSource="Header" Margin="10,0,10,0" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <ContentPresenter.Resources>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="Foreground" Value="{StaticResource Foreground}"/>
                                    </Style>
                                </ContentPresenter.Resources>
                            </ContentPresenter>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="false">
                                <Setter Property="Background" Value="Transparent"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" Value="{StaticResource BorderBrushSelected}"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Background" Value="{StaticResource BackgroundSelected}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Style.Resources>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <Grid KeyboardNavigation.TabNavigation="Local">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Border Background="Transparent" BorderThickness="0,0,0,3" 
                            BorderBrush="{StaticResource BackgroundSelected}">
                        <TabPanel Name="HeaderPanel" Panel.ZIndex="1" Margin="0,0,4,-1" 
                            IsItemsHost="True" KeyboardNavigation.TabIndex="1"/>
                    </Border>
                    <Border Grid.Row="1" Background="{StaticResource Background}"/>
                    <ContentPresenter Grid.Row="1" Name="PART_SelectedContentHost" 
                                      ContentSource="SelectedContent"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


<Style TargetType="{x:Type TabControl}" x:Key="SearchResultsTab">
    <Style.Resources>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid Background="{TemplateBinding Background}" SnapsToDevicePixels="true" Height="23">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="25"/>
                            </Grid.ColumnDefinitions>
                            <ContentPresenter ContentSource="Header" Margin="10,0,10,0" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <ContentPresenter.Resources>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="Foreground" Value="{StaticResource Foreground}"/>
                                    </Style>
                                </ContentPresenter.Resources>
                            </ContentPresenter>
                            <Button Grid.Column="1" Height="15" Width="15" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <Actions:CloseTabItemAction TabItem="{Binding RelativeSource={RelativeSource AncestorType=TabItem}}" 
                                                                    TabControl="{Binding RelativeSource={RelativeSource AncestorType=TabControl}}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                                <Button.Style>
                                    <Style TargetType="{x:Type Button}">
                                        <Setter Property="Background" Value="Transparent"/>
                                        <Setter Property="Cursor" Value="Hand"/>
                                        <Setter Property="Focusable" Value="False"/>
                                        <Setter Property="Template">
                                            <Setter.Value>
                                                <ControlTemplate TargetType="{x:Type Button}">
                                                    <Grid Background="{TemplateBinding Background}">
                                                        <Path x:Name="ButtonPath" 
                                                            Margin="2"
                                                            Data="M0,0 L1,1 M0,1 L1,0"
                                                            Stroke="{StaticResource CloseButtonStroke}" 
                                                            StrokeThickness="2"
                                                            StrokeStartLineCap="Flat"
                                                            StrokeEndLineCap="Flat"
                                                            Stretch="Uniform"
                                                            VerticalAlignment="Center"
                                                            HorizontalAlignment="Center"/>
                                                    </Grid>
                                                    <ControlTemplate.Triggers>
                                                        <MultiDataTrigger>
                                                            <MultiDataTrigger.Conditions>
                                                                <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabItem}}" Value="false"/>
                                                                <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabItem}}" Value="false"/>
                                                            </MultiDataTrigger.Conditions>
                                                            <MultiDataTrigger.Setters>
                                                                <Setter Property="Visibility" Value="Hidden"/>
                                                            </MultiDataTrigger.Setters>
                                                        </MultiDataTrigger>
                                                        <Trigger Property="IsEnabled" Value="False">
                                                            <Setter Property="Visibility" Value="Hidden"/>
                                                        </Trigger>
                                                        <Trigger Property="IsMouseOver" Value="True">
                                                            <Setter Property="Background" Value="{StaticResource CloseButtonBackgroundHighlighted}" />
                                                            <Setter TargetName="ButtonPath" Property="Stroke" Value="{StaticResource CloseButtonStrokeHighlighted}"/>
                                                        </Trigger>
                                                        <Trigger Property="IsPressed" Value="true">
                                                            <Setter Property="Background" Value="{StaticResource CloseButtonBackgroundPressed}"/>
                                                            <Setter TargetName="ButtonPath" Property="Stroke" Value="{StaticResource CloseButtonStroke}"/>
                                                            <Setter TargetName="ButtonPath" Property="Margin" Value="2.5,2.5,1.5,1.5" />
                                                        </Trigger>
                                                    </ControlTemplate.Triggers>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                </Button.Style>
                            </Button>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="false">
                                <Setter Property="Background" Value="Transparent"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" Value="{StaticResource BorderBrushSelected}"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Background" Value="{StaticResource BackgroundSelected}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Style.Resources>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <Grid KeyboardNavigation.TabNavigation="Local">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Border Background="Transparent" BorderThickness="0,0,0,3" 
                            BorderBrush="{StaticResource BackgroundSelected}">
                        <TabPanel Name="HeaderPanel" Panel.ZIndex="1" Margin="0,0,4,-1" 
                            IsItemsHost="True" KeyboardNavigation.TabIndex="1"/>
                    </Border>
                    <Border Grid.Row="1" Background="{StaticResource Background}"/>
                    <ContentPresenter Grid.Row="1" Name="PART_SelectedContentHost" 
                                      ContentSource="SelectedContent"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在在我的主窗口中,我希望我的SearchResults选项卡应用SearchResults样式。现在的情况是,它应用了标准选项卡的样式,如何让我的tabitem指向该特定样式

以下是主窗口中的XAML代码:

<!--All Tabs-->
        <TabControl x:Name="MainWindowTabs"
            Margin="0,10,0,0">

             <TabItem Name="SearchResults" Header="Search Results">
                <GroupBox Header="Two data grids" Margin="50">
                    <StackPanel>
                        <DataGrid 
                            Margin="10" 
                            ItemsSource="{Binding firstData}"/>

                        <DataGrid 
                            Margin="10" 
                            ItemsSource="{Binding secondData}"/>

                    </StackPanel>
                </GroupBox>
            </TabItem> 


我们将非常感谢您的帮助。谢谢

样式的类型为
TabControl
,因此它可以应用于TabControl而不是
TabItem

使用
StaticResource
标记扩展在TabControl上应用样式:

<TabControl x:Name="MainWindowTabs"
            Margin="0,10,0,0"
            Style="{StaticResource SearchResultsTab}">
在资源字典中将样式移出,并给出x:Key:

<Style TargetType="{x:Type TabItem}" x:Key="SearchResultsTabItemStyle">

现在您可以这样使用它:

<TabControl x:Name="MainWindowTabs"
            Margin="0,10,0,0">

   <TabItem Name="SearchResults" Header="Search Results"
            Style="{StaticResource SearchResultsTabItemStyle}">


我所说的平行是指这种结构:

<ResourceDictionary>
   <Style x:Key="StandardTabControl">
      ....
   </Style>
   <Style x:Key="SearchResultsTabItemStyle">
      ....
   </Style>
   <Style x:Key="SearchResultsTab">
      ....
   </Style>
<ResourceDictionary>

....
....
....

现在将该样式应用于我所有的选项卡项,我想要
<ResourceDictionary>
   <Style x:Key="StandardTabControl">
      ....
   </Style>
   <Style x:Key="SearchResultsTabItemStyle">
      ....
   </Style>
   <Style x:Key="SearchResultsTab">
      ....
   </Style>
<ResourceDictionary>