简体   繁体   中英

WPF TabControl: ItemContainerStyle and ItemTemplateSelector

I am not able to find an answer to my question somewhere. Maybe you guys can help me.

I am using WPF and have a TabControl which uses an ItemTemplateSelector. The ItemsSource of the TabControl is an ObservableCollection of Strings. Based on the string, the template for the TabItem is choosen. Works fine so far.

Only problem I have now, is that I want to use a custom style on my TabItems. So I tried the ItemContainerStyle property, but this doesn't work. When I set the ItemContainerStyle, the ItemTemplateSelector doesn't fire anymore. I am not using a ContentTemplateSelector as I don't need this one on this solution as the content of the Tabs is always the same.

So my question is: How can I define the style of the TabItems, when I use an ItemTemplateSelector?

Here is some code:

TabControl on Usercontrol:

<TabControl TabStripPlacement="Right"
            ItemsSource="{Binding loadedPalettes, UpdateSourceTrigger=PropertyChanged}"
            Style="{StaticResource StyleTabControl}"
            ItemTemplateSelector="{StaticResource TabTemplateSelector}"
            Height="Auto"
            SelectionChanged="paletteSelectionChanged"
            SelectedIndex="{Binding selPaletteIndex}"
            Width="Auto"
            Margin="0,5,0,0">

    <TabControl.ContentTemplate>
        <DataTemplate>
            <local:tabDataGrid />
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

TemplateSelector Class

Public Class SSITabTemplateSelector
    Inherits DataTemplateSelector

    Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate
        Dim element As FrameworkElement
        element = TryCast(container, FrameworkElement)

        If element Is Nothing Then Return Nothing
        If container Is Nothing Then Return Nothing

        Select Case item
            Case "Search"
                Return TryCast(element.FindResource("searchTabItem"), DataTemplate)
                'Case "TabSwitch"
                '    Return TryCast(element.FindResource("TextItem"), DataTemplate)
            Case Else
                Return TryCast(element.FindResource("normalTabItem"), DataTemplate)
        End Select

        Return Nothing
    End Function
End Class

DataTemplates for the TabItems

<DataTemplate x:Key="normalTabItem">
    <StackPanel Name="Panel"
                Orientation="Horizontal">
        <TextBlock Text="{Binding}"
                   Background="Transparent">
            <TextBlock.LayoutTransform>
                <RotateTransform Angle="270" />
            </TextBlock.LayoutTransform>
        </TextBlock>

        <ContentPresenter x:Name="ContentSite"
                          VerticalAlignment="Center"
                          HorizontalAlignment="Center"
                          Content="{Binding Content}" />
    </StackPanel>
</DataTemplate>

<DataTemplate x:Key="searchTabItem">
    <StackPanel Name="Panel"
                Orientation="Horizontal">
        <TextBlock Text="blabla"
                   Background="AliceBlue">
            <TextBlock.LayoutTransform>
                <RotateTransform Angle="270" />
            </TextBlock.LayoutTransform>
        </TextBlock>

        <ContentPresenter x:Name="ContentSite"
                          VerticalAlignment="Center"
                          HorizontalAlignment="Center"
                          Content="{Binding Content}" />
    </StackPanel>
</DataTemplate>

Solution:

Yeah, it was that simple. bars222 had the right answer. Just use <TabControl.Resources> and add the styling.

Thanks!

Adding the answer, according the comment. You can write something like this.

<TabControl>
    <TabControl.Resources>
        <Style TargetType="TabItem">
            <Setter Property="Header" Value="MyHeader"/>
        </Style>
    </TabControl.Resources>
</TabControl>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM