How to bind DataGridComboBoxColumn in code

Oct 2, 2008 at 11:41 AM
I have seen the ItemsSource property bound to an ObjectDataProvider in XAML, but I have yet to see how to do it in code.

As the DataGridComboBoxColumn does not derive from Control - there is no SetBinding() method available.

Is there a workaround for this?
Oct 2, 2008 at 12:29 PM

It is just the same as in xaml where you just set the ItemSource in code.  For example,

comboBoxColumn = new DataGridComboBoxColumn();
comboBoxColumn.ItemsSource =
new IEnumerable<T>();


where you replace IEnumerable<T> with the collection that you want to set for the combobox.

Oct 2, 2008 at 12:39 PM
The problem is that my collection will change run-time - that is why I use the ObjectDataProvider.

With a regular ComboBox, I do this:
ComboBox _comboBox = new ComboBox();
Binding _binding = new Binding(){Source = someObjectProvider};
_comboBox.SetBinding(ComboBox.ItemsSourceProperty, _binding);

This is not possible, when the DataGridComboBox does not derive from Control.
Oct 2, 2008 at 1:54 PM
It is still possible using the BindingOperations class.



comboBoxColumn = new DataGridComboBoxColumn();
Binding binding = new Binding { Source = someObjectProvider };
BindingOperations.SetBinding(comboBoxColumn, DataGridComboBoxColumn.ItemsSourceProperty, binding);


Oct 2, 2008 at 5:32 PM
Exactly what I was looking for - thank you very much!