Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use a standard <code>DataGrid</code> to do that. You can find out how to use one on the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid%28v=vs.110%29.aspx" rel="nofollow">DataGrid Class</a> page on MSDN. Normally when using these controls, you would <code>Bind</code> a collection of a custom data type class instances to the <code>DataGrid.ItemsSource</code> property and the <code>DataGrid</code> would add a row for every item in the collection and a column for every property in the data type class.</p> <p>As you want to have a variable number of columns, that would cause you a problem. However, there are something in .NET called <code>Anonymous Types</code> that you can use. This basically enables you to define an anonymous class at run time, so that you can define as many properties as you need columns. You can find out how to use them in the <a href="http://msdn.microsoft.com/en-us/library/bb397696%28v=vs.120%29.aspx" rel="nofollow">Anonymous Types (C# Programming Guide)</a> page on MSDN.</p> <p>My last hint for you is that you'd want to have <code>string</code> properties in your anonymous classes that provide the relevant image paths to use to display whatever image you want in each column. Then, you can declare a <code>DataTemplate</code> to define what each cell should look like (an <code>Image</code>)... for this to work, your <code>string</code> file paths will need to be in the following format -<code>/YourAppName;component/YourImageFolderName/YourImageName.jpg</code>:</p> <pre><code>&lt;DataGrid ItemsSource="{Binding YourAnonymousTypedCollection}"&gt; &lt;DataGrid.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Image Source="{Binding}" /&gt; &lt;/DataTemplate&gt; &lt;/DataGrid.ItemTemplate&gt; &lt;/DataGrid&gt; </code></pre> <p>You can find out more from the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate%28v=vs.110%29.aspx" rel="nofollow"><code>ItemsControl.ItemTemplate</code> Property</a> and <a href="http://msdn.microsoft.com/en-us/library/ms742521%28v=vs.110%29.aspx" rel="nofollow">Data Templating Overview</a> pages on MSDN.</p> <hr> <p>UPDATE >>></p> <p>This is in response to @MartinLiversage's incorrect comment:</p> <p>Martin, really? Yes I do think that you were nit-picking in your unnecessary first comment. Your comment added nothing to the question authors knowledge except maybe doubt and/or confusion, so thanks for that. I personally believe that anyone with half a brain could work out exactly what I meant by <em>define an anonymous class at run time</em>.</p> <p>Just in case that wasn't clear to anyone, I also added the link to the <a href="http://msdn.microsoft.com/en-us/library/bb397696%28v=vs.120%29.aspx" rel="nofollow">Anonymous Types (C# Programming Guide)</a> page on MSDN, so that anyone that didn't understand could find out the whole story. Therefore, your first comment was unnecessary and helped nobody.</p> <p>Moving on to your remarkable second comment where you flat out tell me that I <em>will not be able to solve the "dynamic columns" problem using an anonymous class</em>... this time you are just plain incorrect. You <em>really</em> should check these things before you make such claims. To prove that my method works, I knocked up a quick and easy example:</p> <p>Here is a <code>DataGrid</code>:</p> <pre><code>&lt;DataGrid Name="DataGrid" AutoGenerateColumns="True" /&gt; </code></pre> <p>Here's where I define my anonymous type, put them in a <code>List</code> and <code>Bind</code> them to the <code>DataGrid</code>:</p> <pre><code>var item = new { Id = 1, Name = "Bob", Age = 30 }; var item2 = new { Id = 2, Name = "Jane", Age = 26 }; var item3 = new { Id = 3, Name = "Dave", Age = 42 }; var items = new[] { item, item2, item3 }.ToList(); DataGrid.ItemsSource = items; </code></pre> <p>You can then see that it works perfectly well. Extending this example by creating a type with a different number of parameters shows that it would work with any number of columns:</p> <pre><code>var item = new { Id = 1, Name = "Bob", Age = 30, Date = DateTime.Now }; var item2 = new { Id = 2, Name = "Jane", Age = 26, Date = DateTime.Now }; var item3 = new { Id = 3, Name = "Dave", Age = 42, Date = DateTime.Now }; var items = new[] { item, item2, item3 }.ToList(); DataGrid.ItemsSource = items; </code></pre> <p>Of course the question author could declare the items in a loop to make it more efficient, but I'd like to think that you now realise that you didn't know what you were talking about. Next time you make such a bold claim, I'd advise you to double check that you're correct first so that you don't waste anybody else's time.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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