Amresh Kumar
Amresh Kumar

Reputation: 1445

Creating Data template for ListBox for WindowPhone7

Is it possible to create data-template for the list box in WP7 using C# code instead of XAML??

Upvotes: 1

Views: 1003

Answers (1)

Derek Lakin
Derek Lakin

Reputation: 16319

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.

Upvotes: 2

Related Questions