XAML重构 - 如何提取常见标记

时间:2009-09-19 10:03:19

标签: wpf xaml refactoring

我在这个问题的最后粘贴了一些xaml。它来自我项目中的资源文件。

HierarchicalDataTemplate和DataTemplate共享完全相同的结构。有没有办法提取公共部分并引用它?

<HierarchicalDataTemplate DataType="{x:Type local:ChapterViewModel}"
                          x:Key="ChapterOutcomesTemplate"
                          ItemsSource="{Binding Path=Chapter.Outcomes}"
                          ItemTemplate="{StaticResource AssignedOutcomeTemplate}">
    <StackPanel Orientation="Horizontal">
        <Image Height="16"
            Width="16"
            Margin="0,0,0,0"
            RenderOptions.BitmapScalingMode="NearestNeighbor"
            SnapsToDevicePixels="True"
            Source="{Binding Source={x:Static images:DocumentImages.Outcomes}}"
            Visibility="{Binding IsOutcomesAssigned, Converter={StaticResource BooleanToVisibility}, Mode=OneWay}"
                   />
        <Image Height="16"
            Width="16"
            RenderOptions.BitmapScalingMode="NearestNeighbor"
            SnapsToDevicePixels="True"
            Margin="5,0,0,0"
            Source="{Binding Source={x:Static images:DocumentImages.Chapter}}" 
                   />
        <TextBlock Text="{Binding Chapter.Name}"
            Margin="5,0,0,0" />
    </StackPanel>
</HierarchicalDataTemplate>
<DataTemplate x:Key="ItemTemplate">
    <StackPanel Orientation="Horizontal">
        <Image Height="16"
            Width="16"
            Margin="0,0,0,0"
            RenderOptions.BitmapScalingMode="NearestNeighbor"
            SnapsToDevicePixels="True"
            Source="{Binding Source={x:Static images:DocumentImages.Outcomes}}"
            Visibility="{Binding IsOutcomesAssigned, Converter={StaticResource BooleanToVisibility}, Mode=OneWay}" />
        <Image Height="16"
            Width="16"
            RenderOptions.BitmapScalingMode="NearestNeighbor"
            SnapsToDevicePixels="True"
            Margin="5,0,0,0"
            Source="{Binding Source={x:Static images:DocumentImages.Chapter}}" />
        <TextBlock Text="{Binding Chapter.Name}"
            Margin="5,0,0,0" />
    </StackPanel>

</DataTemplate>

1 个答案:

答案 0 :(得分:3)

是的,你可以。定义将作为控件模板共享的内容,然后在两个模板中使用它:

<!-- New control template -->
<ControlTemplate x:Key="ChapterAndItemTemplate">
  <StackPanel Orientation="Horizontal">
    <Image Height="16" Width="16" Margin="0"
           RenderOptions.BitmapScalingMode="NearestNeighbor"
           SnapsToDevicePixels="True"
           Source="{Binding Source={x:Static images:DocumentImages.Outcomes}}"
           Visibility="{Binding IsOutcomesAssigned, Converter={StaticResource BooleanToVisibility}, Mode=OneWay}" />
    <Image Height="16" Width="16" Margin="5,0,0,0"
           RenderOptions.BitmapScalingMode="NearestNeighbor"
           SnapsToDevicePixels="True"
           Source="{Binding Source={x:Static images:DocumentImages.Chapter}}" />
    <TextBlock Text="{Binding Chapter.Name}" Margin="5,0,0,0" />
  </StackPanel>
</ControlTemplate>

<HierarchicalDataTemplate DataType="{x:Type local:ChapterViewModel}"
                          x:Key="ChapterOutcomesTemplate"
                          ItemsSource="{Binding Path=Chapter.Outcomes}"
                          ItemTemplate="{StaticResource AssignedOutcomeTemplate}">
  <!-- Used here... -->
  <Control Template="{StaticResource ChapterAndItemTemplate}" />
</HierarchicalDataTemplate>

<DataTemplate x:Key="ItemTemplate">
  <!-- ...and here -->
  <Control Template="{StaticResource ChapterAndItemTemplate}" />
</DataTemplate>