2010-02-03 3 views
3

У меня есть следующие DataTemplates:Refactor DataTemplate (XAML), чтобы уменьшить дублирование

Первый:

<DataTemplate DataType="{x:Type WIAssistant:DestinationField}"> 
    <ContentControl Margin="5" MinWidth="60" MinHeight="70" > 
     <Border BorderThickness="2" BorderBrush="Black" CornerRadius="5"> 
      <!--This text block seems un-needed. But it allows the whole control to be dragged. Without it only the border and the 
      text can be used to drag the control.--> 
      <TextBlock> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
        </Grid.RowDefinitions> 
        <TextBlock FontWeight="Bold" TextWrapping="Wrap" HorizontalAlignment="Center" Margin="10" 
           Text="{Binding DestField.Name}"/> 
        <TextBlock Grid.Row="1" TextWrapping="Wrap" HorizontalAlignment="Center" Margin="10" 
           Text="{Binding DestField.FieldType}"/> 
       </Grid> 
      </TextBlock> 
     </Border> 
    </ContentControl> 
</DataTemplate> 

Второй:

<DataTemplate DataType="{x:Type WIAssistant:SourceField}"> 
    <ContentControl Margin="5" MinWidth="60" MinHeight="70" > 
     <Border BorderThickness="2" BorderBrush="Black" CornerRadius="5"> 
      <!--This text block seems un-needed. But it allows the whole control to be dragged. Without it only the border and the 
      text can be used to drag the control.--> 
      <TextBlock> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
        </Grid.RowDefinitions> 
        <TextBlock FontWeight="Bold" TextWrapping="Wrap" HorizontalAlignment="Center" Margin="10" 
           Text="{Binding SrcField.Name}"/> 
        <TextBlock Grid.Row="1" TextWrapping="Wrap" HorizontalAlignment="Center" Margin="10" 
           Text="{Binding SrcField.FieldType}"/> 
       </Grid> 
      </TextBlock> 
     </Border> 
    </ContentControl> 
</DataTemplate> 

Они являются 100% идентичны, за исключением материала, за исключением в {}.

Есть ли способ уменьшить избыточность здесь? Я боюсь, что сделаю изменения в одном и забываю изменить другого.

ответ

4

Что касается вашего комментария в коде - я думаю, проблему можно решить, установив BorderBackground на номер Transparent.

И к основной проблеме. Вероятно, у вас может быть стиль, который описывает ContentControl, который будет содержать DataTemplate для типа свойств SrcField и DestField. Затем привяжите его к имени и типу FieldType и используйте для него основные 2 DataTemplates. Что-то вроде этого:

<Style x:Key="SomeStyle" TargetType="ContentControl"> 
    <Setter Property="Margin" Value="5" /> 
    <Setter Property="MinWidth" Value="60" /> 
    <Setter Property="MinHeight" Value="70" /> 
    <Setter Property="ContentTemplate"> 
     <Setter.Value> 
      <DataTemplate DataType=...> <!-- type of the `SrcField` and `DestField` properties --> 
       <Border Background="Transparent" BorderThickness="2" BorderBrush="Black" CornerRadius="5"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="Auto"/> 
         </Grid.RowDefinitions> 
         <TextBlock FontWeight="Bold" TextWrapping="Wrap" HorizontalAlignment="Center" Margin="10" 
          Text="{Binding Name}"/> 
         <TextBlock Grid.Row="1" TextWrapping="Wrap" HorizontalAlignment="Center" Margin="10" 
          Text="{Binding FieldType}"/> 
        </Grid> 
       </Border> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 



<DataTemplate DataType="{x:Type WIAssistant:DestinationField}"> 
    <ContentControl 
     Style="{StaticResource SomeStyle}" 
     Content="{Binding DestField}" 
     /> 
</DataTemplate> 

<DataTemplate DataType="{x:Type WIAssistant:SourceField}"> 
    <ContentControl 
     Style="{StaticResource SomeStyle}" 
     Content="{Binding SrcField}" 
     /> 
</DataTemplate> 
3

Да, я бы создал пользовательский элемент управления с общедоступными свойствами зависимостей под названием Name и FieldType, а затем использовал этот элемент управления в DataTemplates.

+0

Я хотел бы поддержать это. Можете ли вы отредактировать его, чтобы я мог. («Хромая» функция «препятствует тому, чтобы я формировал upvoting: http://meta.stackexchange.com/questions/21462/undoing-an-old-vote-cannot-be-recast-because-vote-is-too-old) – Vaccano

+0

@vacc done thx. – Will

Смежные вопросы