I saw this question on Stackoverflow
We had this problem some time ago, and to understand the issue we need to recap a little bit, things happens give or take on this order:
-
1-The user select the cell
-
2-change the combobox selection
-
3-the user leave the cell
So, what about the events being fired? I'm pretty sure the one you are interested in is the ComboBox_SelectionChanged right? but the question is WHEN this event is being fired? so let me go straight to the point, I'll use the steps about
-
1-The user select the cell THE CELLS GOES INTO EDIT MODE
-
2-change a value THE ComboBox_SelectionChanged IS FIRED BUT THE CELL STILL IN EDIT MODE AND NO CHANGES ARE PERFORMED-
-
3-the user leave the cell AND THE CHANGES ARE COMMITTED !!
as you can realize the changes are committed only when the cell loses its focus and leave the editing mode, the solution is to commit the changes on the ComboBox_SelectionChanged event, so lets implement our own column type called AutoCommitComboBoxColumn :
here is the code:
public class AutoCommitComboBoxColumn : Microsoft.Windows.Controls.DataGridComboBoxColumn
{
protected override FrameworkElement
GenerateEditingElement(Microsoft.Windows.Controls.DataGridCell cell, object dataItem)
{
var comboBox = (ComboBox)base.GenerateEditingElement(cell, dataItem);
comboBox.SelectionChanged += ComboBox_SelectionChanged;
return comboBox;
}
public void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
CommitCellEdit((FrameworkElement)sender);
}
}
Now go ahead and use this column instead of the default DataGridComboBoxColumn and give it a try.
55f3aa06-f7f0-4713-819e-7d3c1ba76653|0|.0