How can I create a DataTemplate in code (using C#) and then add a control to that DataTemplate?
DataTemplate
Microsoft has a good article over at MSDN: "Data Templating Overview." I would start there.
Update: Eh, scratch that. I read over your requirement for "in code." I'll just leave the link here for whoever might stumble upon this post.