DataGrid: dynamic DataTemplate for dynamic DataGridTemplateColumn

Posted by Lukas Cenovsky on Stack Overflow See other posts from Stack Overflow or by Lukas Cenovsky
Published on 2011-01-10T22:43:06Z Indexed on 2011/01/11 8:53 UTC
Read the original article Hit count: 313

I want to show data in a 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 FirstName { get; set; }
    public string LastName { get; set; }
    ...
}

where the number of candidates in Candidates list varies at runtime.

Desired grid layout looks like this

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

I'd like to have a DataTemplate for each Candidate as I plan changing it during runtime - user can choose what info about candidate is displayed in different columns (candidate is just an example, I have different object). That means I also want to change the column templates in runtime although this can be achieved by one big template and collapsing its parts.

I know about two ways how to achieve my goals (both quite similar):

  1. Use AutoGeneratingColumn event and create Candidates columns
  2. Add Columns manually

In both cases I need to load the DataTemplate from string with XamlReader. Before that I have to edit the string to change the binding to wanted Candidate.

Is there a better way how to create a DataGrid with unknown number of DataGridTemplateColumn?

Note: This question is based on dynamic datatemplate with valueconverter

© Stack Overflow or respective owner

Related posts about wpf

Related posts about Silverlight