重用DataTemplate

时间:2010-10-18 12:13:28

标签: wpf resources datatemplate styles

我的Treelistview控件中有两个DataTemplates用于不同的单元格。 模板有90%的相似性。 如何重用相同的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>

1 个答案:

答案 0 :(得分:6)

您可以尝试这样做:

<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>