Refactor DataTemplate (XAML) для уменьшения дублирования - PullRequest
3 голосов
/ 03 февраля 2010

У меня есть следующие шаблоны данных:

Первый:

<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% идентичны, за исключением материала в {}.

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

Ответы [ 2 ]

4 голосов
/ 03 февраля 2010

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

И к основной проблеме. Возможно, у вас есть стиль, описывающий ContentControl, который будет включать DataTemplate для типа свойств SrcField и DestField. Затем свяжите его с Name и FieldType и используйте его с двумя основными шаблонами данных. Примерно так:

<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 голосов
/ 03 февраля 2010

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

...