数据模板的重用

7

我有两个适用于 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个回答

7
您可以尝试像这样做:
<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>

好主意,但它强制我添加不必要的边框,而我更喜欢避免这种情况。 - Erez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接