Manually instantiate a DataTemplate?
Asked Answered
D

1

5

Is it possible to use a data template for a single item with no ListBox or other items control?

I have a datatemplate that I want to instantiate in xaml, not in a list, just within a border, and set its datacontext.

Something like (pseudo):

<Window DataContext="{StaticResource Contact}">
    <!--
        Here I want to show a ContactTemplate for a single Contact
        as it would appear in an ItemsControl (ListBox etc.)
    -->
    <src:ContactTemplate Source="{Binding}"/>
</Window>
Dangerfield answered 5/9, 2009 at 19:25 Comment(0)
K
9

You can set the ContentTemplate of a ContentControl to a DataTemplate. Is that what you're looking for?

Kilpatrick answered 5/9, 2009 at 20:14 Comment(4)
To spell it out more explicitly: <ContentControl Content="{Binding}" ContentTemplate="{StaticResource ContactTemplate}" />.Christianly
Thank you for the respelling, it was indeed necessary.Dangerfield
Is ContentConrol->ContentTempate the thinnest type to which I can apply DataTemplate?Lundberg
Actually, a ContentPresenter is. And if your DataTemplate is specified solely with a DataType and not a key, you don't even have to specify the template on the ContentPresenter. It's implicit. This is actually how ItemsControld already work. They use a ContentPresenter as part of their ContainerItem's ControlTemplate. It's the ContentPresenter that's actually doing the template-related work.Inflation

© 2022 - 2024 — McMap. All rights reserved.