Is it possible to c开发者_JAVA百科reate data-template for the list box in WP7 using C# code instead of XAML??
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.
精彩评论