WP7 Dynamically Generating DataTemplate in code
published on: 1/31/2011 | Views: N/A | Tags: Silverlight
by WindowsPhoneGeek
This is a quick tip that demonstrates how to dynamically generate DataTemplate in a Windows Phone 7 application. In this example we will use a dynamically created ListBox as well.
Note: For more info about the escape sequence visit the MSDN documentation.
At first lets create a sample method in which we will define our DataTemplate. Note that you can escape the " symbols either by using "" or ' :
private DataTemplate CreateDataTemplate()
{
string xaml =
@"<DataTemplate
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<Grid>
<Rectangle Fill=""LightGreen"" Height=""50"" Width=""250"" />
<TextBlock Text='{Binding}' FontSize='40' />
</Grid>
</DataTemplate>";
DataTemplate dt = (DataTemplate)XamlReader.Load(xaml);
return dt;
}The next step is to add some ItemSource and set the LisBox ItemTemplate in this way:
public MainPage()
{
InitializeComponent();
ListBox list = new ListBox();
list.ItemTemplate = this.CreateDataTemplate();
list.ItemsSource = new List<string>{"first","second","third","forth"};
ContentPanel.Children.Add(list);
}
That`s it. Just build and run your project to see the result.

You can find the full source code here.
Here is one more helpful post by Pete Brown: Dynamically Generating Controls in WPF and Silverlight
You can also follow us on Twitter @winphonegeek
Comments
Excellent!
posted by: CodeMonkey on 10/27/2011 12:16:59 AM
Excellent! Exactly what I was looking for!
feedback
posted by: some name on 3/7/2012 11:12:29 AM
"MSDN documentation" link is wrong.
Great!
posted by: Victor on 4/12/2012 3:26:00 PM
I'm devide my life on 2 stages - one - before WPF, and second - where i know WPF
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
