I have this method call:
public DataTemplate Create(Type type, string propertyName)
{
string str = @"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"" xmlns:local=""clr-namespace:MyProjectName;assembly:MyProjectName""><StackPanel Orientation=""Horizontal""><TextBlock Text=""{Binding propertyLabel}"" FontStyle=""Italic"" Width=""120"" /><TextBox Text=""{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=local:MainPage}, Path=DataContext.Value1}"" Width=""120"" /></StackPanel></DataTemplate>";
DataTemplate _dt = (DataTemplate)XamlReader.Load(str);
return _dt;
}
On calling this i get the next error:
Line: 56
Error: Unhandled Error in Silverlight Application
Code: 2512
Category: ParserError
Message: Failed to create a ‘System.Type’ from the text ‘local:MainPage’.
File:
Line: 1
Position: 253
Scenario is this:
Grid -> DataContext = ViewModel
ListBox -> ItemsSource = ViewModel.MyCollection
ListBoxItem -> DataTemaplate contains { ViewModel.MyCollection.propertyLabel and ViewModel.Value1 }
What’s going on? Why do I get this error? Any ideas are very welcome.
Thanks.
I also couldn’t get past this problem using the ‘clr-namespace:…’ namespace declaration. What I had to do was to declare an xml definition mapping in my code by using the XmlnsDefintionAttribute. A convenient place to put this attribute is in AssemblyInfo.cs. Do not put it inside a namespace block.
You then have to substitute the clr-namespace with the URL in ‘xmlns:local’ XML attribute (3rd line in the sample below)