using one data template in another data template in WPF

Posted by Sowmya on Stack Overflow See other posts from Stack Overflow or by Sowmya
Published on 2011-01-12T03:02:26Z Indexed on 2011/01/12 3:53 UTC
Read the original article Hit count: 292

Filed under:
|

Hi,

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>

and the other is 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: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 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?

Thanks in advance, Sowmya

© Stack Overflow or respective owner

Related posts about wpf

Related posts about DataTemplates