Is it possible to create data-template for the list box in WP7 using C# code instead of XAML??
Asked
Active
Viewed 998 times
1 Answers
2
You can't instantiate a DataTemplate
in code in the same way that you can for regular controls, but you can use the XamlReader.Load()
method to create a DataTemplate
from a XAML string:
string xaml = @"<DataTemplate
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<!-- Template content goes here. -->
</DataTemplate>";
var dt = (DataTemplate)XamlReader.Load(xaml);
Be sure to add any additional namespaces that you might need.
The answer to this question also shows that you can create bindings in the DataTemplate
in the same way: Creating a Silverlight DataTemplate in code.

Community
- 1
- 1

Derek Lakin
- 16,179
- 36
- 51
-
Thanks.. I'll try using the same – Amresh Kumar Jan 27 '11 at 11:45