【问题标题】:Styling TabItem when populated with ItemsSource使用 ItemsSource 填充 TabItem 样式
【发布时间】:2011-01-24 02:37:45
【问题描述】:

我正在使用一个 WPF Tabcontrol,其中填充了一个使用 Itemssource 的集合。

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

..但是ItemContainerStyle,当然会覆盖控件模板。

如何将这两者结合起来,以便我可以动态加载我的 tabcontrol 并且仍然能够按照我想要的方式设置我的 TabItems 样式?

【问题讨论】:

    标签: wpf tabcontrol controltemplate tabitem itemssource


    【解决方案1】:

    好的...解决了我自己的问题。很明显..

    命名我的模板

    <Style TargetType="{x:Type TabItem}" x:Key="TabItemTemplate">
    

    添加了一个像这样的 BasedOn 属性:

    <Style TargetType="TabItem" BasedOn="{StaticResource TabItemTemplate}">
    

    但如果我可以将它们组合成一个模板,请告诉我...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 2012-01-09
      • 2020-11-28
      • 2018-10-04
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      相关资源
      最近更新 更多