How do you compose DataTemplates / link to a child datatemplate in WPF?
Here's the problem.
Given a large/intricate datatemplate A, which has 3 sections - General, Properties, Misc. Imagine 3 grids for each. Now I need to reuse the Properties section of the above Datatemplate in another place. Reasons: To avoid redundancy + ensure that further updates to the datatemplate are applied identically to all usages.So I guess what I am asking for is an ability to slot in a l开发者_如何转开发ink to a child DataTemplate in a parent Datatemplate. What's the best way to go about this ?
I found one way to do this.. but I'm not sure if its the right way or the best.. Posting it as an answer below so that it can be rated.
I used a ContentPresenter to slot in a 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>
精彩评论