DataGrid and Cell-Level ComboBoxes

Paxenos Source

Scenario

ComboBox C depends on the selected value of ComboBox B, which depends on the selected value of ComboBox A. All of these ComboBoxes are in a DataGrid.

Common Road Blocks:

  1. The User must be able to add new rows (This Requires a ItemsSource item type that has a parameterless constructor).
  2. To access the database to populate the List of Available Options for the Comboboxes, the current project would require the Database Credentials/DataContext be passed into the Constructor.

Attempt 1

I've tried using 3 CollectionViewSources, one for each of the ComboBoxes (Concept Code Here), but the SelectedItem of the ComboBox gets automatically selected in the other DataGrid rows. I need to find a way to isolate the CollectionViewSource to each row.

I've considered just adding the CollectionViewSource data to each of the DataGrid Items so I can just bind to it that way, but I have to access the database to generate the CollectionViewSource.

I also tried not sharing the CollectionViewSource as seen in this question, but that destroyed the link between the 3 ComboBoxes, as well as the Rows. If I could just set the CollectionViewSources to be shared within each DataGrid Row and not between each of them, I think it would work. I just can't find a way to do that.

Attempt 2

I've looked at this question: How to get cell level ComboBox for WPF DataGrid?

This would work, but the User needs to be able to add rows to the DataGrid. The example code in that question also uses a parameterless constructor. I am in a situation where access to the database to populate the lists would have to be passed into the constructor.

The Question

How do I do this correctly?

wpfdatagridcombobox

Answers

answered 7 years ago Rachel #1

Bind to a List or ObservableCollection, not a CollectionViewSource

CollectionViewSource tracks the Current Item, so changing the value in one ComboBox will change it in all ComboBoxes

comments powered by Disqus