ContentPresenter DataTemplate
Asked Answered
M

2

10

I have a DataTemplate:

<DataTemplate x:Key="myTemplate">
    ...
</DataTemplate>

I want to use it as the ContentTemplate of a ContentPresenter:

<ContentPresenter Content="{Binding X}">
    <ContentPresenter.ContentTemplate >
        <!-- ????? what goes here ????-->
    </ContentPresenter.ContentTemplate>
</ContentPresenter>

How can I use the predefined DataTemplate in my ContentPresenter?

Merciless answered 11/4, 2011 at 9:43 Comment(0)
E
16

You just need to reference the defined resource:

<ContentPresenter Content="{Binding X}" ContentTemplate="{StaticResource myTemplate}"/>
Evermore answered 11/4, 2011 at 9:44 Comment(0)
I
8

Should be something like:

<ContentPresenter Content="{Binding X}" ContentTemplate="{StaticResource myTemplate}"/>

Although I wouldn't define a Template on a contentpresenter, I will select the correct template based on the type anyway.

Intervention answered 11/4, 2011 at 9:46 Comment(2)
i cant upvote your answer for some reason... it says i already votedMerciless
Two reasons for having templates on content presenter: 1. Provide separate view and edit template for the same object. 2. Split the view into two parts, e.g in a treeviewitem template you can show one part before the ItemsPresenter and one part after.Retarded

© 2022 - 2024 — McMap. All rights reserved.