I try to make a ListView with dynamic generation of column. I use mvvm patern. How i can implement this? In this momemt I have only static columns.
You can create GridView with appropriate columns dynamically using converter. Here is working example:

MainWindow.xaml
MainWindow.xaml.cs
using System.Collections.Generic;
using System.Windows;
namespace WpfApplication1
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
public class ViewModel
{
public ColumnConfig ColumnConfig { get; set; }
public IEnumerable Products { get; set; }
public ViewModel()
{
Products = new List { new Product { Name = "Some product", Attributes = "Very cool product" }, new Product { Name = "Other product", Attributes = "Not so cool one" } };
ColumnConfig = new ColumnConfig { Columns = new List { new Column { Header = "Name", DataField = "Name" }, new Column { Header = "Attributes", DataField = "Attributes" } } };
}
}
public class ColumnConfig
{
public IEnumerable Columns { get; set; }
}
public class Column
{
public string Header { get; set; }
public string DataField { get; set; }
}
public class Product
{
public string Name { get; set; }
public string Attributes { get; set; }
}
}
ConfigToDynamicGridViewConverter.cs
using System;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Data;
namespace WpfApplication1
{
public class ConfigToDynamicGridViewConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var config = value as ColumnConfig;
if (config != null)
{
var grdiView = new GridView();
foreach (var column in config.Columns)
{
var binding = new Binding(column.DataField);
grdiView.Columns.Add(new GridViewColumn {Header = column.Header, DisplayMemberBinding = binding});
}
return grdiView;
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
}