If you can specify the columns instead of auto-generating them, this could be very easy.
Here's an example:
<DataGrid ItemsSource="{Binding Employees}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding EmployeeName}"/>
<!-- Displays the items of the first collection-->
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding Dogs}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Displays the items of the second collection-->
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding Cats}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
ViewModel:
public class MainWindowViewModel : NotificationObject
{
public MainWindowViewModel()
{
Employees = new ObservableCollection<Employee>
{
new Employee { EmployeeName = "Steven"},
new Employee { EmployeeName = "Josh"},
};
}
public ObservableCollection<Employee> Employees { get; set; }
}
Models:
public class Employee
{
public Employee()
{
Dogs = new ObservableCollection<Dog>
{
new Dog { Gender = 'M'},
new Dog { Gender = 'F'},
};
Cats = new ObservableCollection<Cat>
{
new Cat { Name = "Mitzy" , Kind = "Street Cat"},
new Cat { Name = "Mitzy" , Kind = "House Cat"}
};
}
public string EmployeeName { get; set; }
public ObservableCollection<Dog> Dogs { get; set; }
public ObservableCollection<Cat> Cats { get; set; }
}
public class Dog
{
public char Gender { get; set; }
public override string ToString()
{
return "Dog is a '" + Gender + "'";
}
}
public class Cat
{
public string Name { get; set; }
public string Kind { get; set; }
public override string ToString()
{
return "Cat name is " + Name + " and it is a " + Kind;
}
}
Consider ItemsCollectionA
as Employees
and ItemsCollectionB
and ItemsCollectionC
as Dogs
and Cats
. It still uses the ToString
to display the Dogs
and Cats
object's that I overridden, But you can simply set a DataTemplate
to the listBox's within the columns to decide how to display your models. Also notice the AutoGenerateColumns="False"
on the DataGrid
to avoid creating the columns twice.
Hope this helps