Problem:
I have a DataTable with lookup column into another table. Another table is a small table so all the records can fit in small combobox and the user wants to be able to select lookup record with combobox.
Solution:
XAML has DataGridComboBoxColumn, but it is difficult to bind, because DataGridComboBoxColumn can’t bind to Windows binding source. It is obviously quite know problem, but I couldn’t find an easy solution.
Test tables:
MAIN_TABLE { ID, SubTableID, ... other fields ... } SUB_TABLE { ID, Description }
Naive
Naive solution, that doesn’t work
<DataGrid ItemsSource="{Binding mainTable}"> <DataGrid.Columns> <DataGridTextColumn Header="ID" Binding="{Binding ID}" /> <DataGridTextColumn Header="SubID" Binding="{Binding SubTableID }" /> <DataGridComboBoxColumn Header="Desc" ItemsSource="{Binding subTable}}" <!-- can't bind! --> SelectedValueBinding="{Binding SubTableID}" SelectedValuePath="ID" DisplayMemberPath="Description" /> </DataGrid.Columns> </DataGrid>
The solution doesn’t work because for some reason DataGridComboBoxColumn can’t bind subTable even if it is defined exactly as mainTable in datasource for the whole xaml window.
Solution
The solution is quite easy. Create a resource that binds to sub table and then use that resurce sa data source 🙂
<DataGrid ItemsSource="{Binding mainTable}"> <DataGrid.Resources> <CollectionViewSource x:Key="cvsSubTable" Source="{Binding subTable }"/> <!-- can bind! --> </DataGrid.Resources> <DataGrid.Columns> <DataGridTextColumn Header="ID" Binding="{Binding ID}" /> <DataGridTextColumn Header="SubID" Binding="{Binding SubTableID }" /> <DataGridComboBoxColumn Header="Desc" ItemsSource="{Binding Source={StaticResource cvsSubTable }}" <!-- now it is ok! --> SelectedValueBinding="{Binding SubTableID}" SelectedValuePath="ID" DisplayMemberPath="Description" /> </DataGrid.Columns> </DataGrid>
Lost 4 hours on that! Grrrr..