How to bind DataGridComboBoxColumn in code

Oct 2, 2008 at 12:41 PM
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?
Coordinator
Oct 2, 2008 at 1:29 PM

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

DataGridComboBoxColumn
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 1: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.
Coordinator
Oct 2, 2008 at 2:54 PM
It is still possible using the BindingOperations class.

DataGridComboBoxColumn

 

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

 

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