简体   繁体   中英

Customize TabItem DataTemplate

How can I make DataTemplate in WPF for TabItems and in each TabItem customize its content?

I need this:

<TabControl>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <Label Content="Name" Name="label1" />
                    <TextBox Name="name" />
                    ...
                </DataTemplate>
            </TabControl.ContentTemplate>
            <TabItem Header="Add" Name="tabItem1">
                <Grid Height="213">
                    <Button Content="Add" Name="button1" />
                </Grid>
            </TabItem>
            <TabItem Header="Edit" Name="tabItem2">
                <Grid>
                    <Button Content="Edit" Name="button2" />
                </Grid>
            </TabItem>
</TabControl>

but the buttons are not displaying (only content of DataTemplate).

Put the reoccurring template as a resource in the TabControl, and then reference it from the specific Tab's ContentTemplate using a ContentPresenter :

<TabControl>
    <TabControl.Resources>
        <DataTemplate x:Key="TabTemplate">
            <Label Content="Name" Name="label1" />                
        </DataTemplate>
    </TabControl.Resources>
    <TabItem Header="Add" Name="tabItem1">
        <TabItem.ContentTemplate>
            <DataTemplate>
                <Grid Height="213">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Button Content="Add" Name="button1" />
                    <ContentPresenter Grid.Row="1" ContentTemplate="{StaticResource TabTemplate}"/>
                </Grid>
            </DataTemplate>
        </TabItem.ContentTemplate>
    </TabItem>
    <TabItem Header="Edit" Name="tabItem2">
        <TabItem.ContentTemplate>
            <DataTemplate>
                <Grid Height="213">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Button Content="Edit" Name="button2" />
                    <ContentPresenter Grid.Row="1" ContentTemplate="{StaticResource TabTemplate}"/>
                </Grid>
            </DataTemplate>
        </TabItem.ContentTemplate>
    </TabItem>

Everything besides the ContentPresenter can be different in every tab...

You could also try using TabControl ContentTemplateSelector .

Here, you could define two or more different templates (user controls) for a tab item. And you could also decide this on run time using DataTemplateSelector

Here is a blog which contain a demo of the same : mvvm-using-contenttemplateselector-in-tab-control-view/

The final TabControl would look something like this:

<UserControl x:Class="TabControlItemTemplateDemo.View.MyTabControl"
         xmlns:ViewModel="clr-namespace:TabControlItemTemplateDemo" 
         xmlns:View1="clr-namespace:TabControlItemTemplateDemo.View" mc:Ignorable="d" >

<UserControl.Resources>
    <DataTemplate x:Key="MyTabHeaderTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Text="{Binding Header}" Width="80" Height="25" FontWeight="Bold"/>
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="CountryContentTemplate">
        <View1:CountryView DataContext="{Binding CurrentMyTabContentViewModel}"/>
    </DataTemplate>

    <DataTemplate x:Key="ContinentsContentTemplate">
        <View1:ContinentsView DataContext="{Binding CurrentMyTabContentViewModel}"/>
    </DataTemplate>

    <ViewModel:MyViewSelector x:Key="selector" 
                                       CountryTemplate="{StaticResource CountryContentTemplate}"
                                       ContintentsTemplate="{StaticResource ContinentsContentTemplate}" />
</UserControl.Resources>
<DockPanel>
    <TabControl ItemsSource="{Binding Tabs}" TabStripPlacement="Left" 
                BorderThickness="0" Background="White"  
                ItemTemplate="{StaticResource MyTabHeaderTemplate}"
                ContentTemplateSelector="{StaticResource selector}">
    </TabControl>
</DockPanel>

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