How do you link DataTemplates / reference to child datatemplate in WPF?

Here's the problem.
Considering a large / complex dataset A, which has 3 sections - General, Properties, Miscellaneous. Imagine 3 grids for each.
Now I need to reuse the Properties section of the above Datatemplate elsewhere. Reasons: To avoid redundancy + make sure further updates on the datatemplate are applied equally across all usages.

So my guess what I'm asking is is the ability to break the reference to the child DataTemplate in the parent Datatemplate. What's the best way to do this?

I found one way to do it .. but I'm not sure if this is the right way or the best one. Posting it as an answer below so it can be appreciated.

+2


a source to share


1 answer


I used ContentPresenter to create a slot in the child datatemplate via its ContentTemplate property.



// child
<DataTemplate x:Key="propertiesVMTemplate">
    <toolkit:DataGrid Style= ....  // lots of stuff here
    </toolkit:DataGrid>
</DataTemplate>

// parent
<DataTemplate x:Key="nodeVMTemplate">
    ... general section
        // and the link
        <ContentPresenter Content="{Binding Properties}" ContentTemplate="{StaticResource propertiesVMTemplate}"/>

        ...misc section stuff
</DataTemplate>

      

+3


a source







All Articles