user2837961
user2837961

Reputation: 1555

Multiple lists in binding DataGrid

I have two ObservableCollection

List1 = new ObservableCollection<ManagementFunctionModel>();

List2 = new ObservableCollection<ManagementFunctionModel>();

Both these lists have their own data. In my xaml, is it possible to bind my Datagrid to two the two different lists? Ideally I want to bind List1 on button1click and List2 on button2click. My xaml is below

<DataGrid Name="grid" HorizontalAlignment="Stretch" ItemsSource="{Binding List1}" Margin="0,0,0,50" VerticalAlignment="Stretch" AutoGenerateColumns="False" CanUserAddRows="False">
  <DataGrid.Columns>
    ...
    ...
  </DataGrid.Columns>
</DataGrid>

Upvotes: 1

Views: 1163

Answers (1)

Mike Eason
Mike Eason

Reputation: 9713

You cannot set the ItemsSource twice on a single DataGrid to two different lists.

You can however create a new custom class which will hold both of your ManagementFunctionModel objects in a single class:

public class ManagementFunctionPair
{
   public ManagementFunctionModel First { get; set; }
   public ManagementFunctionModel Second { get; set; }
}

Then, use an ObservableCollection of ManagementFunctionPair, and bind that to your DataGrid instead:

list = new ObservableCollection<ManagementFunctionPair>();

Upvotes: 1

Related Questions