xaml 如何为两个选项卡控件创建样式

发布于 2024-10-24 19:28:34 字数 1430 浏览 0 评论 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 使用不同的样式?示例代码会很有帮助。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2024-10-31 19:28:34

您已经定义了一个针对“TabItem”的单一样式,该样式将应用于任何选项卡控件的选项卡项,因此两个选项卡控件将具有相同的样式。您可以使用 x:Key 属性来命名两种不同的样式,然后根据您的要求将它们应用到 TabControl 或 TabItem。

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.

红墙和绿瓦 2024-10-31 19:28:34

您可以像下面的代码一样,为各个选项卡指定样式,并为样式资源提供键

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文