C# – dynamic datatemplate with valueconverter

cdatatemplatevalueconverterwpfxaml

I want to show data in a wpftoolkit datagrid where the data is a collection of

public class Thing
{
    public string Foo { get; set; }
    public string Bar { get; set; }
    public List<Candidate> Candidates { get; set; }
}

public class Candidate
{

    public string Name { get; set; }
    public CandidateType CandidateType { get; set; }
}

public enum CandidateType
{
    Type1,
    Type2,
    Type42
}

where the number of candidates in Candidates list is configurable at runtime.

Desired grid layout looks like this

Foo | Bar | Candidate 1 | Candidate 2 | ... | Candidate N

Thus it seems I cannot create a DataTemplate for the candidates in xaml as the binding expression will change.

I add necessary columns in AutoGeneratedColumns event like so:

private void DataGrid_AutoGeneratedColumns(object sender, EventArgs e)
{
    ViewModel vm = DataContext as ViewModel;

    for (int i = 0; i < vm.LotsOfThings.First().Candidates.Count; i++)
    {
        string assName = Assembly.GetExecutingAssembly().GetName().Name;
        ParserContext ctx = new ParserContext();
        ctx.XamlTypeMapper = new XamlTypeMapper(new string[] { assName });
        ctx.XamlTypeMapper.AddMappingProcessingInstruction("src", "WpfToolkitDataGridTester", assName);
        ctx.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
        ctx.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
        ctx.XmlnsDictionary.Add("src", "clr-namespace:WpfToolkitDataGridTester;assembly=" + assName);
        var template = XamlReader.Parse(@"<DataTemplate>
                                            <DataTemplate.Resources>
                                                <src:FooConverter x:Key='fooConverter' />
                                            </DataTemplate.Resources>
                                            <TextBlock  
                                                Foreground='{Binding Candidates[" + i + @"].CandidateType,Converter={StaticResource fooConverter}}'
                                                Text='{Binding Candidates[" + i + @"].Name}' />
                                        </DataTemplate>", ctx) as DataTemplate;
        dg.Columns.Add(new DataGridTemplateColumn
        {
            Header = "Candidate " + (i + 1),
            CellTemplate = template
        });
    }
}

This however fails with the following exception:
The tag 'FooConverter' does not exist in XML namespace 'clr-namespace:WpfToolkitDataGridTester;assembly=WpfToolkitDataGridTester'. Line '3' Position '54'.

Changing the StaticResource to DynamicResource makes no change.

What am I missing?

FWIW: a hardcoded datatemplate

<DataTemplate x:Key="candidateTemplate">
  <DataTemplate.Resources>
    <src:FooConverter x:Key="fooConverter" />
  </DataTemplate.Resources>
  <TextBlock 
    Foreground="{Binding Candidates[0].CandidateType,Converter={StaticResource fooConverter}}"
    Text="{Binding Candidates[0].Name}" />
</DataTemplate>

and the template column defined like so

<wpftk:DataGridTemplateColumn CellTemplate="{StaticResource candidateTemplate}" />

'works' but obviously does not produce the desired result as Candidates[0] is hardcoded.

Best Answer

For whatever reason, it works as expected if I do like this...

    string assName = Assembly.GetExecutingAssembly().GetName().Name;
    StringBuilder sb = new StringBuilder();
    sb.Append("<DataTemplate ");
    sb.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
    sb.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
    sb.Append("xmlns:src='clr-namespace:WpfToolkitDataGridTester;assembly=" + assName + "' >");
    sb.Append("<DataTemplate.Resources>");
    sb.Append("<src:FooConverter x:Key='fooConverter' />");
    sb.Append("</DataTemplate.Resources>");
    sb.Append("<TextBlock ");
    sb.Append("Foreground='{Binding Candidates[" + i + "].CandidateType,Converter={StaticResource fooConverter}}' ");
    sb.Append("Text='{Binding Candidates[" + i + @"].Name}' />");
    sb.Append("</DataTemplate>");
    var template = (DataTemplate)XamlReader.Parse(sb.ToString());
Related Topic