Refactor DataTemplate (XAML) to reduce duplication

I have the following data types:

The first:

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

Second:

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

They are 100% identical, except for the material in {}.

Is there a way to reduce redundancy here? I am afraid that I will make changes in one and forgot to change the other.

+3
source share
2 answers

As for your comment in the code - I think the problem can be solved by setting Border Backgroundto 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>
+4

, Name FieldType, DataTemplates.

+3

Source: https://habr.com/ru/post/1731259/


All Articles