DataTemplate with code converter

I am trying to load a DataTemplate into code, but it works fine if I delete the Converter ... but as soon as I put it there it hit. Now I set my state in my namespace and put a link to my converter in XAML. eg:

<Window.Resources> <local:StatCellConverter x:Key="myConverter" /> </Window.Resources> 

and this is my method when I create a 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><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; } 

What am I doing wrong? Maybe I would also have to define my converter in code?

My converter

 public class StatCellConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { Debug.WriteLine(value); if (value.Equals("#DIV/0#")) return "0"; return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 

I get an exception saying that it cannot load a DataTemplate

+4
source share
1 answer

Actually this is a mistake in the structure. Adding a local namespace via XmlnsDictionary will not work. It should be added to the template definition with the definition of the assembly and namespace:

as in the comment above @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; } 
+3
source

All Articles