开发者

xaml How to create a style for two tabcontrol

<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"
                   Content开发者_开发技巧Source="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>

That code will create two TabControls with the same style. How can I make those two TabControls use different styles? Sample code would be helpful.


You have defined a single style targetting "TabItem" which will apply to the tab items of any tab control, thus both your tab controls will have the same style. You can use the x:Key attribute to name two different styles and then apply them to the TabControl or TabItem as per your requirements.


You can either do like in the code below,specifieing style for individual tabitems giving a key for the style resource

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

or you can use control level style resources

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜