xaml 如何为两个选项卡控件创建样式
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经定义了一个针对“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.
您可以像下面的代码一样,为各个选项卡指定样式,并为样式资源提供键
,或者您可以使用控制级样式资源
You can either do like in the code below,specifieing style for individual tabitems giving a key for the style resource
or you can use control level style resources