DataTemplate with Converter in Code Behind
This in fact is a bug in the framework. Adding the local name space through the XmlnsDictionary wouldn't work. It has to be added within the template definition with the assembly and namespace defined:
as in the comment above by @Nerd In Training this should work:
string statRowTemplate = "<DataTemplate >";
private DataTemplate GenerateStatRowDataTemplate()
{
ParserContext pc = new ParserContext();
pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
pc.XmlnsDictionary.Add("xcdg", "http://schemas.xceed.com/wpf/xaml/datagrid");
string statRowTemplate = "<DataTemplate xmlns:local=\"clr-namespace:MyTest;assembly=MyTest\" ><xcdg:StatRow>";
statRowTemplate += "<xcdg:StatCell FieldName=\"Column4\" ResultPropertyName=\"AvgColumn4\">";
statRowTemplate += "<xcdg:StatCell.ContentTemplate>";
statRowTemplate += "<DataTemplate>";
statRowTemplate += "<TextBlock Text=\"{Binding ., Converter={StaticResource ResourceKey=myConverter}}\" />";
statRowTemplate += "</DataTemplate>";
statRowTemplate += "</xcdg:StatCell.ContentTemplate>";
statRowTemplate += "</xcdg:StatCell>";
statRowTemplate += "</xcdg:StatRow>";
statRowTemplate += "</DataTemplate>";
StringReader stringReader = new StringReader(statRowTemplate);
XmlReader xmlReader = XmlReader.Create(stringReader);
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(statRowTemplate.ToString()));
DataTemplate dt = (DataTemplate)XamlReader.Load(ms,pc);
dt.LoadContent();
return dt;
}