在WPF中的另一个数据模板中使用一个数据模板 [英] using one data template in another data template in WPF

查看:63
本文介绍了在WPF中的另一个数据模板中使用一个数据模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据模板,其中一个是另一个的子集,如下所示:

I have two data templates, one of which is the subset of another like below:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:igEditors="http://infragistics.com/Editors"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:controls="clr-namespace:Client.UI.WPF;assembly=Client.UI.WPF"
xmlns:d="http://schemas.microsoft.com/expression/blend/2006"
>
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/Client.Resources.WPF.Styles;Component/Styles/CommonStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="XYZDataTemplate">
    <Grid x:Name="_rootGrid" DataContext="{Binding DataContext}" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
                    <controls:ValueDisplay Grid.Row="0" Grid.Column="0" LabelText="Build number" x:Name="buildNumber" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" 
                                   Margin="5,10,0,0">
            <igEditors:XamTextEditor  />
        </controls:ValueDisplay>
        <controls:ValueDisplay  Grid.Row="0" Grid.Column="1" LabelText="Tool version" x:Name="toolVersion" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" 
                                    Margin="20,10,0,0">
            <igEditors:XamTextEditor IsReadOnly="True"/>
        </controls:ValueDisplay>
               </Grid>

</DataTemplate>

,另一个如下所示:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:igEditors="http://infragistics.com/Editors"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:controls="clr-namespace:BHI.ULSS.Client.UI.WPF;assembly=ULSS.Client.UI.WPF"
xmlns:d="http://schemas.microsoft.com/expression/blend/2006"
>


<DataTemplate x:Key="ABCDataTemplate" >
    <Grid x:Name="_rootGrid" DataContext="{Binding DataContext}" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <controls:ValueDisplay Grid.Row="0" Grid.Column="0" LabelText="Build number" x:Name="buildNumber" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" 
                                   Margin="5,10,0,0">
            <igEditors:XamTextEditor  />
        </controls:ValueDisplay>
        <controls:ValueDisplay  Grid.Row="0" Grid.Column="1" LabelText="Tool version" x:Name="toolVersion" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" 
                                    Margin="20,10,0,0">
            <igEditors:XamTextEditor IsReadOnly="True"/>
        </controls:ValueDisplay>
        <controls:ValueDisplay Grid.Row="0" Grid.Column="2" LabelText="Size" ShowUnit="True" x:Name="size" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" 
                                   Margin="20,10,0,0">
            <igEditors:XamTextEditor/>
        </controls:ValueDisplay>
               </Grid>

</DataTemplate>

XYZDataTemplate是以下内容的子集ABCDataTemplate作为两个数据模板中的前两个字段是通用的,所以我想知道是否可以用XYZDataTemplate的ABCDataTemplate替换冗余代码以实现代码可维护性?有人可以建议这是否是一个正确的方法,如果可以的话,我如何实现?

XYZDataTemplate is a subset of the ABCDataTemplate as the first two fields in both the data templates are common, so I was wondering if it is possible to replace the redundant code in the ABCDataTemplate with that of the XYZDataTemplate for code maintainability? Could anyone please suggest if would this be a right approach, if so how can I acheive that?

在此先感谢
Sowmya

Thanks in advance, Sowmya

推荐答案

如果XAML中有一些样板,则可以使用 ContenPresenter 作为宏。 ,以在多个地方扩展样板。首先,定义 DataTemplate ,然后使用 ContentPresenter 和资源键来扩展宏。下面是一个示例:

If you have some boilerplate in XAML, you can use ContenPresenter as a sort of "macro" to expand your boilerplate in multiple places. First you define a DataTemplate and then you use the ContentPresenter with the resource key to "expand" the macro. Here is an example:

<Grid>
    <Grid.Resources>
        <DataTemplate x:Key="boilerplate">
            <StackPanel Orientation="Horizontal">
                <Rectangle Width="100" Height="100" Stroke="Black" Fill="{Binding}"/>
                <Rectangle Width="100" Height="100" Stroke="Black" Fill="{Binding}"/>
            </StackPanel>
        </DataTemplate>
    </Grid.Resources>
    <StackPanel>
        <ContentPresenter ContentTemplate="{StaticResource boilerplate}" Content="Red"/>
        <ContentPresenter ContentTemplate="{StaticResource boilerplate}" Content="Blue"/>
    </StackPanel>
</Grid>

由于模板是真实的模板,因此可以使用数据绑定。可以将其视为仅包含一项的 ItemsControl 。如果没有绑定,则可以省略 Content 属性。您可以将其视为宏的参数。

As the template is a real template you can use data binding. Think of it as an ItemsControl with just one item. If there is no binding you can omit the Content property. You can think of it as the macro "parameter".

过度使用此选项将使您的XAML难以阅读,并且具有适度的性能成本,因此请谨慎使用。最后,存在一些局限性,因为宏总是扩展为一个顶级元素,因此您不能将两个元素添加到单个 Panel 中,而一次性使用 ContentPresenter

Over-using this will make your XAML harder to read and it has a modest performance cost so use it carefully. Finally, there are some limitations in that the "macro" always expands to one top-level element so you cannot add two elements to a single Panel with a single use of ContentPresenter.

这篇关于在WPF中的另一个数据模板中使用一个数据模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆