2016-01-20 79 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> 
相關問題