Рефакторинг XAML - Как извлечь общую разметку - PullRequest
2 голосов
/ 19 сентября 2009

У меня есть некоторые 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 Ответ

3 голосов
/ 19 сентября 2009

Да, вы можете. Определите содержимое, которое должно быть передано в качестве шаблона элемента управления, а затем используйте его в обоих шаблонах:

<!-- 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...