2010-02-03 72 views
3

我有以下的DataTemplates:重构的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

至于你在代码中的评论 - 我认为这个问题可以通过设置BorderBackgroundTransparent来解决。

还有主要问题。您可能有一种描述ContentControl的样式,其中将包含SrcFieldDestField属性类型的DataTemplate。然后将其绑定到Name和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

我想upvote这个。你可以编辑它,所以我可以。 (一个蹩脚的“功能”阻止我形成upvoting:http://meta.stackexchange.com/questions/21462/undoing-an-old-vote-cannot-be-recast-because-vote-is-too-old) – Vaccano 2010-02-04 17:06:33

+0

@vacc完成thx。 – Will 2010-02-04 18:31:10