如何为WPF中的多个TabItem设置一次样式?

时间:2016-01-20 22:36:08

标签: wpf

我是WPF的新手,但我认为有一种方法可以设置多个TabItems来使用相同的Style,而无需单独为每个TabItem添加Style。就像我在第一个TabItem中所做的那样。这可能吗?

\\A\\s*\\z

我的TabItems样式是:

<TabControl Grid.Row="0" x:Name="tabControl" Margin="5,0,5,5" Height="600" Width="998">
   <TabItem x:Name="tabSetToRun" Header="Run" Style="{DynamicResource myTabItemStyle}"/>

   <TabItem x:Name="tabShortcut" Header="Freeze Shortcut"/>
   <TabItem x:Name="tabFullAccess" Header="Full Access"/>
   <TabItem x:Name="tabOldForms" Header="Old Forms"/>
   <TabItem x:Name="tabCFG" Header="CFG Files"/>
</TabControl>

2 个答案:

答案 0 :(得分:3)

资源查找从可视树中的元素向上发生。 如果Parse.Cloud.useMasterKey();没有Style,则会将其应用于所有类型的x:Key

TargetType

<Window x:Class="TabItemMultiple.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TabItemMultiple" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style TargetType="{x:Type TabItem}"> <Setter Property="Foreground" Value="White"/> <Setter Property="Width" Value="180"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TabItem}"> <Grid> <Border Name="Border" Background="#FF293955" BorderBrush="LightCyan"/> <ContentPresenter x:Name="ContentSite" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="Foreground" Value="Black"/> <Setter Property="Panel.ZIndex" Value="100" /> <Setter TargetName="Border" Property="Background" Value="LightCyan" /> <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <TabControl Grid.Row="0" x:Name="tabControl" Margin="5,0,5,5" Height="600" Width="998"> <TabItem x:Name="tabSetToRun" Header="Run" /> <TabItem x:Name="tabShortcut" Header="Freeze Shortcut"/> <TabItem x:Name="tabFullAccess" Header="Full Access"/> <TabItem x:Name="tabOldForms" Header="Old Forms"/> <TabItem x:Name="tabCFG" Header="CFG Files"/> </TabControl> </Grid> </Window> 中删除x:Key。如果您将Style放入Style的{​​{1}}集合中,则会更改Resources中的Window

同样,如果您将其放入TabItem Window集合中,那么它们将在整个应用程序中进行更改。

如果您不想在Application中更改每个Resources,也可以这样做:

TabItem

请注意,Window现已退回,我们使用<Window x:Class="TabItemMultiple.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TabItemMultiple" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style x:Key="myTabItemStyle" TargetType="{x:Type TabItem}"> <Setter Property="Foreground" Value="White"/> <Setter Property="Width" Value="180"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TabItem}"> <Grid> <Border Name="Border" Background="#FF293955" BorderBrush="LightCyan"/> <ContentPresenter x:Name="ContentSite" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="Foreground" Value="Black"/> <Setter Property="Panel.ZIndex" Value="100" /> <Setter TargetName="Border" Property="Background" Value="LightCyan" /> <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <TabControl Grid.Row="0" x:Name="tabControl" Margin="5,0,5,5" Height="600" Width="998"> <TabControl.Resources> <Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource myTabItemStyle}" /> </TabControl.Resources> <TabItem x:Name="tabSetToRun" Header="Run" /> <TabItem x:Name="tabShortcut" Header="Freeze Shortcut"/> <TabItem x:Name="tabFullAccess" Header="Full Access"/> <TabItem x:Name="tabOldForms" Header="Old Forms"/> <TabItem x:Name="tabCFG" Header="CFG Files"/> </TabControl> </Grid> </Window>

Result

答案 1 :(得分:1)

我还不能对答案发表评论,但以下内容也有效。它基于Szabolcs的答案https://stackoverflow.com/a/34912002/5786449

它稍微简单一点,不需要你的风格的键,仍然只适用于TabItem的这个实例。但它不太可重复使用。

<TabControl Grid.Row="0" x:Name="tabControl" Margin="5,0,5,5" Height="600" Width="998">
    <TabControl.Resources>
        <Style x:Key="myTabItemStyle" TargetType="{x:Type TabItem}">
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Width" Value="180"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid>
                            <Border 
                            Name="Border"
                            Background="#FF293955"
                            BorderBrush="LightCyan"/>
                            <ContentPresenter x:Name="ContentSite"                                    
                                VerticalAlignment="Stretch"
                                HorizontalAlignment="Stretch"
                                ContentSource="Header"
                                Margin="12,2,12,2"
                                RecognizesAccessKey="True"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="FontWeight" Value="Bold"/>
                                <Setter Property="Foreground" Value="Black"/>
                                <Setter Property="Panel.ZIndex" Value="100" />
                                <Setter TargetName="Border" Property="Background" Value="LightCyan" />
                                <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.Resources>
    <TabItem x:Name="tabSetToRun" Header="Run" />
    <TabItem x:Name="tabShortcut" Header="Freeze Shortcut"/>
    <TabItem x:Name="tabFullAccess" Header="Full Access"/>
    <TabItem x:Name="tabOldForms" Header="Old Forms"/>
    <TabItem x:Name="tabCFG" Header="CFG Files"/>
</TabControl>