Wpf 填充ItemsSource时的样式选项卡Item
我使用的是一个WPF选项卡控件,其中填充了一个使用Itemssource的集合Wpf 填充ItemsSource时的样式选项卡Item,wpf,tabcontrol,controltemplate,tabitem,itemssource,Wpf,Tabcontrol,Controltemplate,Tabitem,Itemssource,我使用的是一个WPF选项卡控件,其中填充了一个使用Itemssource的集合 <TabControl x:Name="_tabControl" ItemsSource="{Binding TabViewModelList}"> <TabControl.ItemContainerStyle> <Style TargetType="TabItem">
<TabControl x:Name="_tabControl" ItemsSource="{Binding TabViewModelList}">
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="Header" Value="{Binding TabCaption}"/>
<Setter Property="Content" Value="{Binding TabContent}"/>
<Setter Property="IsSelected" Value="{Binding IsDefault}"/>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
现在我想在App.xaml(或其他资源文件)中设置TabItem样式,如下所示:
<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.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="LightBlue" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="Background" Value="LightGray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="TabItem" BasedOn="{StaticResource TabItemTemplate}">
…但是ItemContainerStyle当然会覆盖controltemplate
如何将这两者结合起来,以便动态加载tabcontrol,并且仍然能够按照我想要的方式设置TabItems的样式?好的。。。解决了我自己的问题。很明显 命名我的模板
<Style TargetType="{x:Type TabItem}" x:Key="TabItemTemplate">
添加了BasedOn属性,如下所示:
<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.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="LightBlue" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="Background" Value="LightGray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="TabItem" BasedOn="{StaticResource TabItemTemplate}">
但如果我能把它们组合成一个模板,请让我知道