我是新来的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>
当我使用 我得到一个错误:Style对象不允许影响其适用 –
JimDel
对象的样式属性是,你不能从Style中改变Style。我的意思是将你的'Setter .... />'myTabItemStyle'复制到新的'Style'中:' 。请用'myTabItemStyle'的定义更新问题。 –
那么你是说我应该直接在tabControl中添加所有的Style信息?使用你的? –
JimDel