代码之家  ›  专栏  ›  技术社区  ›  Vaccano

重构数据模板(XAML)以减少重复

  •  3
  • Vaccano  · 技术社区  · 15 年前

    我有以下数据模板:

    第一个:

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

    它们完全相同,除了中的内容。

    有没有办法减少这里的冗余度?我担心我会改变其中一个,而忘记改变另一个。

    2 回复  |  直到 15 年前
        1
  •  4
  •   arconaut    15 年前

    至于您在代码中的评论,我认为可以通过设置 Border Background Transparent .

    以及主要问题。您可能有一个描述 ContentControl ,其中将包含 SrcField DestField 性质。然后将其绑定到name和fieldtype,并使用它作为主要的2个数据模板。像这样:

    <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>
    
        2
  •  3
  •   Will    15 年前

    是的,我将创建一个具有名为name和fieldtype的公共依赖属性的用户控件,然后在数据模板中使用该控件。