Hexie
Hexie

Reputation: 4221

Inheriting / BasedOn datatemplate

I have a DataTemplate that I am using within my WPF application -

<DataTemplate x:Key="mattersTemplate">
    <Border Name="border" BorderBrush="Aqua" BorderThickness="1" Padding="5" Margin="5">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0" Text="FileRef:"/>
            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=FileRef}" />
            <TextBlock Grid.Row="1" Grid.Column="0" Text="Description:"/>
            <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Description}"/>
            <TextBlock Grid.Row="2" Grid.Column="0" Text="Priority:"/>
            <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=Priority}"/>
        </Grid>
    </Border>
</DataTemplate>

I then (in a DocumentSetTemplateSelector class) define which template to use;

What I would like to do / know is; Create 4 other templates that would inherit this above template AND then allow certain attributes to be over-written;

An example (this template inherits from the above class) - so they look the same;

<DataTemplate x:Key="documentSet_Accounting">
    <ContentPresenter Content="{Binding mattersTemplate}" 
         ContentTemplate="{StaticResource mattersTemplate}">
    </ContentPresenter>
</DataTemplate>

I would like for there to be a style attached to this (if possible) so to get this effect;

<DataTemplate x:Key="documentSet_Accounting">
    <ContentPresenter fontsize="20" Content="{Binding mattersTemplate}" 
         ContentTemplate="{StaticResource mattersTemplate}">
    </ContentPresenter>
</DataTemplate>

or

<DataTemplate x:Key="documentSet_Accounting">
    <ContentPresenter Style="AccountingStyle" Content="{Binding mattersTemplate}" 
         ContentTemplate="{StaticResource mattersTemplate}">
    </ContentPresenter>
</DataTemplate>

Upvotes: 4

Views: 949

Answers (1)

Stephen Holt
Stephen Holt

Reputation: 2368

How about using style inheritance within the templates rather than template inheritance?

<Style x:Key="mattersTemplateStyle">
    <Setter Property="TextBlock.Foreground" Value="Green"/>
</Style>
<Style x:Key="documentSet_AccountingStyle" BasedOn="{StaticResource mattersTemplateStyle}">
    <Setter Property="TextBlock.FontSize" Value="20"/>            
</Style>
<DataTemplate x:Key="mattersTemplate">
    <Border Name="border" BorderBrush="Aqua" BorderThickness="1" Padding="5" Margin="5">
        <Grid Style="{StaticResource mattersTemplateStyle}">
            [...]
        </Grid>
    </Border>
</DataTemplate>
<DataTemplate x:Key="documentSet_Accounting">
    <Grid Style="{StaticResource documentSet_AccountingStyle}">
        <ContentPresenter Content="{Binding mattersTemplate}" ContentTemplate="{StaticResource mattersTemplate}"></ContentPresenter>
    </Grid>
</DataTemplate>

Upvotes: 1

Related Questions