2009-09-19 40 views
2

我在這個問題的最後粘貼了一些xaml。它來自我的項目中的資源文件。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> 

回答

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

嗨德魯。你是否知道有任何工具可以幫助完成這樣的重構?類似於resharper如何在代碼中做它.. Cheers – Berryl 2009-10-07 02:16:58

+0

@Berryl - 我還沒有看到任何工具,但我相信他們很快就會出現。對於想要試用ReSharper插件API的人來說,這可能是一個有趣的項目。 – 2009-10-07 05:57:20

+0

@DrewNoakes我想這不會是明智的繼續等待:-) – itsho 2015-04-13 17:18:50