Note that there are some explanatory texts on larger screens.

plurals
  1. PODataGrid: dynamic DataTemplate for dynamic DataGridTemplateColumn
    primarykey
    data
    text
    <p>I want to show data in a datagrid where the data is a collection of</p> <pre><code>public class Thing { public string Foo { get; set; } public string Bar { get; set; } public List&lt;Candidate&gt; Candidates { get; set; } } public class Candidate { public string FirstName { get; set; } public string LastName { get; set; } ... } </code></pre> <p>where the number of candidates in Candidates list varies at runtime.</p> <p>Desired grid layout looks like this</p> <pre><code>Foo | Bar | Candidate 1 | Candidate 2 | ... | Candidate N </code></pre> <p>I'd like to have a <code>DataTemplate</code> for each <em>Candidate</em> 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.</p> <p>I know about two ways how to achieve my goals (both quite similar):</p> <ol> <li>Use <code>AutoGeneratingColumn</code> event and create <em>Candidates</em> columns</li> <li>Add Columns manually</li> </ol> <p>In both cases I need to load the <code>DataTemplate</code> from string with <code>XamlReader</code>. Before that I have to edit the string to change the binding to wanted <em>Candidate</em>.</p> <p><strong>Is there a better way how to create a DataGrid with unknown number of DataGridTemplateColumn?</strong></p> <p><em>Note:</em> This question is based on <a href="https://stackoverflow.com/questions/652304/dynamic-datatemplate-with-valueconverter">dynamic datatemplate with valueconverter</a></p> <p><strong>Edit:</strong> As I need to support both WPF and Silverlight, I've created my own <code>DataGrid</code> component which has <code>DependencyProperty</code> for bindig a collection of columns. When the collection changes, I update the columns.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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