重构 DataTemplate (XAML) 以减少重复

发布于 2024-08-19 19:43:11 字数 2538 浏览 4 评论 0原文

我有以下数据模板:

第一个:

<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% 相同。

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

I have the following datatemplates:

First One:

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

<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 stuff in the {}.

Is there a way to reduce the redundancy here? I fear I will make a change in one and forget to change the other.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2024-08-26 19:43:11

至于您在代码中的评论 - 我认为可以通过将 BorderBackground 设置为 Transparent 来解决问题。

并解决主要问题。您可能有一个描述 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>

As for your comment in the code - i think the problem can be solved by setting the Border's Background to Transparent.

And to the main problem. You could probably have a style that describes the ContentControl, which will include the DataTemplate for the type of the SrcField and DestField properties. Then bind in it to Name and FieldType, and use it the main 2 DataTemplates. Something like this:

<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>
萌化 2024-08-26 19:43:11

是的,我将创建一个具有名为 Name 和 FieldType 的公共依赖属性的用户控件,然后在 DataTemplates 中使用该控件。

Yeah, I'd create a user control with public dependency properties called Name and FieldType and then use that control in your DataTemplates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文