1
votes

I'm fairly new to WPF, and I'm having a difficulty understanding the subtlety of DataTemplate, DataContext and ofcourse - MVVM. My problem is this: I want to create a DataTemplate for the class Person - which contains a grid which contains three lables - for the First Name, Last Name and Age. After creating my DataTemplate, I wish to add a person to the main grid in the mainwindow, without a listbox or a component which has ItemSource.

To sum up my question - I want to know if there's a way to visualize objects with DataTemplate in which i dont have to add them to a collection and then to ItemsSource.

Thanks!

1

1 Answers

0
votes

What you need is a ContentPresenter, this element displays the data specified by its Content property using the DataTemplate specified by its ContentTemplate property. So, in your example, you can use the following:

<ContentPresenter ContentTemplate={StaticResource YourTemplate}
                  Content={Binding PathToYourPerson}/>

For reference, this is the element that controls such as Buttons use within their template to render their content.