如何在WPF中为多个选项卡项设置一次样式?

如何在WPF中为多个选项卡项设置一次样式?,wpf,Wpf,我是WPF新手,但我认为有一种方法可以将多个TabItems设置为使用相同的样式,而无需将样式单独添加到每个TabItem。就像我在第一项中所做的那样。这可能吗 <TabControl Grid.Row="0" x:Name="tabControl" Margin="5,0,5,5" Height="600" Width="998"> <TabItem x:Name="tabSetToRun" Header="Run" Style="{DynamicResource my

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

<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>

我对TabItems的风格是:

     <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>

资源查找从可视化树中的元素向上进行。 如果
样式
没有
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
现在又回来了,我们使用
BasedOn


资源查找从可视化树中的元素向上进行。 如果
样式
没有
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
现在又回来了,我们使用
BasedOn


我还不能对答案发表评论,但以下内容也可以。这是基于萨博尔茨的回答

它稍微简单一些,不需要样式上的键,仍然只适用于
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>

我还不能对答案发表评论,但以下内容也可以。这是基于萨博尔茨的回答

它稍微简单一些,不需要样式上的键,仍然只适用于
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>


当我使用时,我得到一个错误:Style对象不允许影响它所应用对象的Style属性是的,您不能从
样式更改
样式。我的意思是将您的
myTabItemStyle
复制到新的
样式中:
。请用
myTabItemStyle
的定义更新问题。那么你是说我应该直接将所有样式信息添加到tabControl下?用你的?啊,我明白了。我删除了x:Key=“myTabItemStyle”,这就成功了。我现在也更好地理解了这一切是如何结合在一起的。非常感谢。当我使用时,我得到一个错误:Style对象不允许影响它应用到的对象的Style属性是的,您不能从
样式更改
样式。我的意思是将您的
myTabItemStyle
复制到新的
样式中:
。请用
myTabItemStyle
的定义更新问题。那么你是说我应该直接将所有样式信息添加到tabControl下?用你的?啊,我明白了。我删除了x:Key=“myTabItemStyle”,这就成功了。我现在也更好地理解了这一切是如何结合在一起的。非常感谢。