dimanche 10 mai 2015

TabControl with multiple ControlTemplates (WPF)

In my project I have multiple file types opened at the same time, for example an image, a text file and a video. These are displayed in a TabControl, which allows the user to open and close files.

Depending on the file type, I need to have different controls in my TabViewItem - e.g. a Play/pause button for a video, some editing buttons for a text and some tools for photos.

I tried to use DataTemplate:

<Window.Resources>
    <local:TypeToImageConverter x:Key="TypeToImageConverter" />       
    <DataTemplate x:Key="TabHeaderTemplate">
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding type, Converter={StaticResource TypeToImageConverter}}" Height="16"/>
            <Label Content={Binding filename}/>
            <Button Click="CloseTab">
                <Image Source="/Icons/closeWindow.ico" Height="16"/>
            </Button>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="TabContentTemplate">
        <!--Some Controls-->
    </DataTemplate>
    <Style x:Key="TabItemContainerStyle" TargetType="TabItem">
        <Setter Property="HeaderTemplate" 
                Value="{StaticResource TabHeaderTemplate}"/>
        <Setter Property="ContentTemplate" Value="{StaticResource TabContentTemplate}"/>
    </Style>
</Window.Resources>

<Dock Panel>
    <TabControl Name="TabControlMain" ItemContainerStyle="{StaticResource TabItemContainerStyle}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
</Dock Panel>

In the code behind, a List of Documents (each containing type and filename) is set as ItemsSource.

For the Header this works fine: An icon depending on the file type is displayed.

But how can I add controls in the Content of each TabViewItem depending on the filetype? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire