2016-01-20 87 views
0

我是新来的WPF,但我会认为有一种方法,我可以设置多个TabItems使用相同的样式,而无需逐个添加样式到每个TabItem。就像我在第一个TabItem中完成的一样。这可能吗?如何为WPF中的多个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> 

回答

3

资源查找从元件发生向上的可视化树。 如果一个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> 

Style取出x:Key。如果您将Style放入您的WindowResources集合中,它将更改,即Window

同样,如果你把它放在你的ApplicationResources集合中,那么它们将在整个应用程序中被改变。

你也可以这样做,如果你不想每次TabItemWindow改变:

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

注意,x:Key现在回到我们使用BasedOn

Result

+0

当我使用我得到一个错误:Style对象不允许影响其适用 – JimDel

+0

对象的样式属性是,你不能从Style中改变Style。我的意思是将你的'Setter .... />'myTabItemStyle'复制到新的'Style'中:' 。请用'myTabItemStyle'的定义更新问题。 –

+0

那么你是说我应该直接在tabControl中添加所有的Style信息?使用你的? – JimDel

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> 
相关问题