You can use a standard DataGrid
to do that. You can find out how to use one on the DataGrid Class page on MSDN. Normally when using these controls, you would Bind
a collection of a custom data type class instances to the DataGrid.ItemsSource
property and the DataGrid
would add a row for every item in the collection and a column for every property in the data type class.
As you want to have a variable number of columns, that would cause you a problem. However, there are something in .NET called Anonymous Types
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 Anonymous Types (C# Programming Guide) page on MSDN.
My last hint for you is that you'd want to have string
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 DataTemplate
to define what each cell should look like (an Image
)... for this to work, your string
file paths will need to be in the following format -/YourAppName;component/YourImageFolderName/YourImageName.jpg
:
<DataGrid ItemsSource="{Binding YourAnonymousTypedCollection}">
<DataGrid.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" />
</DataTemplate>
</DataGrid.ItemTemplate>
</DataGrid>
You can find out more from the ItemsControl.ItemTemplate
Property and Data Templating Overview pages on MSDN.
Just in case that wasn't clear to anyone, I also added the link to the Anonymous Types (C# Programming Guide) 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.
Moving on to your remarkable second comment where you flat out tell me that I will not be able to solve the "dynamic columns" problem using an anonymous class... this time you are just plain incorrect. You really should check these things before you make such claims. To prove that my method works, I knocked up a quick and easy example:
Here is a DataGrid
:
<DataGrid Name="DataGrid" AutoGenerateColumns="True" />
Here's where I define my anonymous type, put them in a List
and Bind
them to the DataGrid
:
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;
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:
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;
Of course the question author could declare the items in a loop to make it more efficient.