tags:

views:

317

answers:

2

How to create a GridView like view to the WPF ListView if i dont know the Column names at the design time because the collection will be created at runtime and the collection will be of type DataTable and it may contain a no.of columns.I can do it with WPF DataGrid but i want to achieve this with WPF ListView and I want to Achieve this without code behind.

A: 

This is with Code-Behind

But you can modify it somehow to work from a ViewModel and a Inherit from a ListView Control that accepts a DataTable as a Binding source.

You can generate the GridViewColumns for the ListView programatically and apply the binding to that. Loop through the columns in the DataSet and add a corresponding GridViewColumn into the ListView.

var gridView = (GridView)list.View;
foreach(var col in table.Columns) {
   gridView.Columns.Add(new GridViewColumn{
      Header=col.ColumnName, 
      DisplayMemberBinding=new Binding(col.ColumnName)});
}
Soni Ali
+1  A: 

You can add columns dynamically to a ListView by using Attached Properties. Check out this article on the CodeProject it explains exactly that...

WPF DynamicListView - Binding to a DataMatrix

Tawani