Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing of DataTemplate

I have two DataTemplates for different cells in my Treelistview control. the templates are 90% similar. How can I reuse the identical Xaml ?

  <DataTemplate x:Key="@names">
            <TextBlock Name="txt" Text="{Binding Names}" Style="{DynamicResource @BasicTextBlockStyle}"
                    Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border},AncestorLevel=1}, Path=BorderBrush,Mode=OneWay}" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding IsVisible}" Value="True">
                    <Setter Property="Style" Value="{DynamicResource @HeaderTextBlockStyle}" TargetName="txt" />
                </DataTrigger>
                <DataTrigger Binding="{Binding IsScoped}" Value="True">
                    <Setter Property="Style" Value="{DynamicResource @RootElementTextBlockStyle}" TargetName="txt" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <DataTemplate x:Key="@Ages">
            <TextBlock Name="txt" Text="{Binding Age}" Style="{DynamicResource @BasicTextBlockStyle}"
                    Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border},AncestorLevel=1}, Path=BorderBrush,Mode=OneWay}" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding IsVisible}" Value="True">
                    <Setter Property="Style" Value="{DynamicResource @HeaderTextBlockStyle}" TargetName="txt" />
                </DataTrigger>
                <DataTrigger Binding="{Binding IsScoped}" Value="True">
                    <Setter Property="Style" Value="{DynamicResource @RootElementTextBlockStyle}" TargetName="txt" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
like image 859
Erez Avatar asked Oct 18 '10 12:10

Erez


1 Answers

You can try doing something like this:

<DataTemplate x:Key="Inner">
    <TextBlock Name="txt" Text="{Binding}" Style="{DynamicResource @BasicTextBlockStyle}"
            Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border},AncestorLevel=1}, Path=BorderBrush,Mode=OneWay}" />
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding IsVisible}" Value="True">
            <Setter Property="Style" Value="{DynamicResource @HeaderTextBlockStyle}" TargetName="txt" />
        </DataTrigger>
        <DataTrigger Binding="{Binding IsScoped}" Value="True">
            <Setter Property="Style" Value="{DynamicResource @RootElementTextBlockStyle}" TargetName="txt" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

<DataTemplate x:Key="Names">
    <Border BorderBrush="Black" BorderThickness="1" Height="100" Width="100" HorizontalAlignment="Left">
        <ContentPresenter Content="{Binding Names}" ContentTemplate="{StaticResource Inner}" />
    </Border>
</DataTemplate>

<DataTemplate x:Key="Ages">
    <Border BorderBrush="Black" BorderThickness="1" Height="100" Width="100" HorizontalAlignment="Right">
        <ContentPresenter Content="{Binding Ages}" ContentTemplate="{StaticResource Inner}" />
    </Border>
</DataTemplate>
like image 73
mdm20 Avatar answered Oct 06 '22 19:10

mdm20