public partial class DataTemplates : Window
{
public DataTemplates()
{
InitializeComponent();
var myList = new List();
myList.Add(DateTime.Now);
myList.Add(50);
myList.Add("Hello World");
DataContext = myList;
}
}
Result:
As you can see, there's no reason at all to use code to manipulate UI elements in WPF (except some very very specific cases)
Edit:
Note that you don't usually create a DataTemplate for classes inside the System namespace (such as System.String. This is only to give you an example. If you really needed this you would probably have to create a ViewModel for each type.