Note that there are some explanatory texts on larger screens.

plurals
  1. PODataTemplate with Converter in Code Behind
    primarykey
    data
    text
    <p>I'm trying to load a DataTemplate in code-behind, but it works fine if I remove the Converter... but as soon as I put it in there, it blows. Now, I did set my state my namespace and placed the reference to my converter in XAML. for example:</p> <pre><code>&lt;Window.Resources&gt; &lt;local:StatCellConverter x:Key="myConverter" /&gt; &lt;/Window.Resources&gt; </code></pre> <p>and this is my method where I generate a DataTemplate:</p> <pre><code>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 = "&lt;DataTemplate&gt;&lt;xcdg:StatRow&gt;"; statRowTemplate += "&lt;xcdg:StatCell FieldName=\"Column4\" ResultPropertyName=\"AvgColumn4\"&gt;"; statRowTemplate += "&lt;xcdg:StatCell.ContentTemplate&gt;"; statRowTemplate += "&lt;DataTemplate&gt;"; statRowTemplate += "&lt;TextBlock Text=\"{Binding ., Converter={StaticResource ResourceKey=myConverter}}\" /&gt;"; statRowTemplate += "&lt;/DataTemplate&gt;"; statRowTemplate += "&lt;/xcdg:StatCell.ContentTemplate&gt;"; statRowTemplate += "&lt;/xcdg:StatCell&gt;"; statRowTemplate += "&lt;/xcdg:StatRow&gt;"; statRowTemplate += "&lt;/DataTemplate&gt;"; 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; } </code></pre> <p>What am i doing wrong? Could it be that I would have to define my converter in code behind as well?</p> <p>My Converter</p> <pre><code>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(); } } </code></pre> <p>I get an exception saying that it cannot load the DataTemplate</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload