2011-03-21 45 views
0
<Window.Resources> 
    <Style TargetType="{x:Type TabItem}"> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TabItem}"> 
      <Grid> 
       <Border 
       Name="Border" 
       Background="LightBlue" 
       BorderBrush="Black" 
       BorderThickness="1,1,1,1" 
       CornerRadius="6,6,0,0" > 
       <ContentPresenter x:Name="ContentSite" 
        VerticalAlignment="Center" 
        HorizontalAlignment="Center" 
        ContentSource="Header" 
        Margin="12,2,12,2"/> 
       </Border> 
      </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
    </Style> 
    </Window.Resources> 
    <Grid> 
     <TabControl Height="181" VerticalAlignment="Top"> 
     <TabItem Header="Cheese" /> 
     <TabItem Header="Pepperoni" /> 
     <TabItem Header="Mushrooms" /> 
     </TabControl> 
     <TabControl Margin="0,201,0,60"> 
     <TabItem Header="Cheese" /> 
     <TabItem Header="Pepperoni" /> 
     <TabItem Header="Mushrooms" /> 
     </TabControl> 
    </Grid> 

該代碼將創建兩個具有相同樣式的TabControl。我如何讓這兩個TabControl使用不同的樣式?示例代碼會很有幫助。xaml如何爲兩個tabcontrol創建樣式

回答

0

您已經定義了一個適用於任何選項卡控件的選項卡的「TabItem」樣式,因此您的選項卡控件都將具有相同的樣式。您可以使用x:Key屬性來命名兩種不同的樣式,然後根據您的要求將它們應用於TabControl或TabItem。

0

您可以不喜歡在下面的代碼,specifieing個人的TabItems給人一種關鍵的樣式資源

<Window.Resources> 
    <Style TargetType="{x:Type TabItem}" x:Key="TabItemStyle1"> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TabItem}"> 
      <Grid> 
      ------------ 
      ------------ 
      </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
    </Style> 
    <Style TargetType="{x:Type TabItem}" x:Key="TabItemStyle2"> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TabItem}"> 
      <Grid> 
      ------------ 
      ------------ 
      </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
    </Style> 
    </Window.Resources> 
<Grid> 
     <TabControl Height="181" VerticalAlignment="Top"> 
     <TabItem Header="Cheese" Style="{StaticResource TabItemStyle1} /> 
     <TabItem Header="Pepperoni" Style="{StaticResource TabItemStyle1} /> 
     <TabItem Header="Mushrooms" Style="{StaticResource TabItemStyle1} /> 
     </TabControl> 
     <TabControl Margin="0,201,0,60"> 
     <TabItem Header="Cheese" Style="{StaticResource TabItemStyle2} /> 
     <TabItem Header="Pepperoni" Style="{StaticResource TabItemStyle2} /> 
     <TabItem Header="Mushrooms" Style="{StaticResource TabItemStyle2} /> 
     </TabControl> 
    </Grid> 

,或者您可以使用控制級別樣式資源

<Grid> 
      <TabControl Height="181" VerticalAlignment="Top"> 
     <TabControl.Resources> 
     <Style TargetType="{x:Type TabItem}"> 
      <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type TabItem}"> 
       <Grid> 
       ------------ 
       ------------ 
       </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
      </Setter> 
     </Style> 
</TabControl.Resources> 
      <TabItem Header="Cheese" /> 
      <TabItem Header="Pepperoni" /> 
      <TabItem Header="Mushrooms" /> 
      </TabControl> 
      <TabControl Margin="0,201,0,60"> 
<TabControl.Resources> 
     <Style TargetType="{x:Type TabItem}"> 
      <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type TabItem}"> 
       <Grid> 
       ------------ 
       ------------ 
       </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
      </Setter> 
     </Style> 
</TabControl.Resources> 
      <TabItem Header="Cheese" /> 
      <TabItem Header="Pepperoni" /> 
      <TabItem Header="Mushrooms" /> 
      </TabControl> 
     </Grid> 
風格
相關問題