简体   繁体   中英

Set Header of TabItem

I have a TabControl which is bound to a List of UserControls (MyControls)

  <TabControl Background="{x:Null}" x:Name="MyView" ItemsSource="{Binding MyControls}" >

I want to bind the header of each tab item to a property(Title) in each UserControl . Which I did as below

<TabControl.ItemContainerStyle>
                 <Style TargetType="TabItem">
                    <Setter Property="Header" Value="{Binding Title}"/>                   
                </Style>
</TabControl.ItemContainerStyle>

However since I override the ItemContainerStyle I lost all the default style for the application. My tab header looks different from other tab headers in the application

Is there any way to just bind to the Title without changing any style?

Define an ItemTemplate :

<TabControl Background="{x:Null}" x:Name="MyView" ItemsSource="{Binding MyControls}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Title}" />
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

MyControls shouldn't return an IEnumerable<UserControl> though. It should return an IEnumerable<YourObject> where YourObject is a POCO class with a Title property along with any other properties. You should then use DataTemplates to define the appearance of a YourObject .

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