0

I Have a user control which has label and Data Grid.I need to populate this Label and Datagrid and add it to my Listview in MainWindow.I need to add multiple instances of my User control to the Listview as each and every time I get different data and the no of columns of Datagrid varies. I am really clueless as where to begin.Any links or help would be great.

The Data to the label and Datagrid is filled Dynamically from my code. Here is my code to fill the Datagrid dynamically

  public partial class MainWindow : Window
{
public ObservableCollection<Record> records {get; set;}
public MainWindow()
{
    InitializeComponent();
    DataContext = this
    DisplayGrid();
}

private void DisplayGrid()
{
    records  = new ObservableCollection<Record>();
    records.Add(new Record(new Property("FirstName", "ABC"), new Property("LastName", "DEF")));
    records.Add(new Record(new Property("FirstName", "GHI"), new Property("LastName", "JKL")));

    var columns = records.First()
        .Properties
        .Select((x, i) => new { Name = x.Name, Index = i })
        .ToArray();

    foreach (var column in columns)
    {
        var binding = new Binding(string.Format("Properties[{0}].Value", column.Index));
        dataGrid.Columns.Add(new DataGridTextColumn() { Header = column.Name, Binding = binding });
    }
}

}

and in my usercontrol

    <DataGrid
    Name="dataGrid"
    AutoGenerateColumns="False"
    ItemsSource="{Binding Path=records}"/>

This is the first instance.I want to repeat it for multiple instances.

How can i do it ??

Thanks

Rohit
  • 10,056
  • 7
  • 50
  • 82

0 Answers0