views:

589

answers:

1

I've defined a DataTemplate in a ResourceDictionary. The template need some data (for populating a ListBox). Previously the template was a UserControl and the data was provided by setting the DataContext property.

Is there some way of using code-behind for a DataTemplate or is using an ObjectDataProvider to provide the data the only option?

+2  A: 

You don't provide data to the DataTemplate, it's the other way around : you provide a DataTemplate to display some data. For instance, use a ContentControl, and set its Content to the data you want to display :

<ContentControl Content="{Binding SomeData}" />

The ContentControl will pick the adequate DataTemplate based on the type of the data. You can also specify the DataTemplate explicitly by using the ContentTemplate property of the ContentControl

Thomas Levesque