In my C# WPF application (.NET 4.0) I have a DataGrid dynamically filled from code including a DataGridComboBoxColumn:
public static DataGridComboBoxColumn getCboCol(string colName, Binding textBinding)
{
List<string> statusItemsList = new StatusList();
DataGridComboBoxColumn cboColumn = new DataGridComboBoxColumn();
cboColumn.Header = colName;
cboColumn.SelectedItemBinding = textBinding;
cboColumn.ItemsSource = statusItemsList;
return cboColumn;
}
Using the BeginningEdit event different checks are performed.
If the checks return okay, I want to expand the combobox directly, otherwise edit mode is cancelled:
void dataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
...
if(notOK)
e.Cancel;
else {
DataGridComboBoxColumn dgCboCol = (DataGridComboBoxColumn)e.Column;
// expand dgCboCol
}
...
}
Questions: How to expand the combobox programmatically? Is BeginningEdit event the right place to do that?
Answer:
void dataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
if (e.EditingElement.GetType().Equals(typeof(ComboBox)))
{
ComboBox box = (ComboBox)e.EditingElement;
box.IsDropDownOpen = true;
}
}