DataGridComboBoxColumn enter edit mode on select?

Apr 7, 2009 at 8:00 AM
I have a data entry form which mainly features a DataGrid with a set of columns, the most prominent one being a DataGridComboBoxColumn which after some work I seem to have got to work mostly properly. However, for the user to need to press F2 to enter edit mode for each row before they can provide this one piece of information, then Enter and F2 again for the next row, is clearly suboptimal. Ideally, for this column, I would like the cell to enter edit mode immediately when it receives focus. If that is not easily achieveable, I could live with all cells in that column being in 'edit' mode all the time (replacing the regular cell style with the editing cell style, as long as the user can interact with the ComboBox).

I looked for the styles for DataGridComboBoxColumn, but all I found was the <Style x:Key="{x:Type dg:DataGridComboBoxColumn+TextBlockComboBox}"> in DataGrid\Themes\Generic.xaml, which doesn't seem to come even close to being the full style.

Could someone please point me in the right direction?
Apr 8, 2009 at 1:57 PM
Apr 11, 2009 at 2:39 PM
Does the single click editing sample under the tips and tricks not provide the functionality you needd for this scenario?
Apr 14, 2009 at 7:04 AM
Not really (as I understood the code, at least), because the cell can receive focus in ways other than by mouse click, and I want the same behavior regardless of how focus is obtained. However, I was able to somewhat resolve the issue by hooking into the DataGrid's CurrentCellChanged event and calling DataGrid.BeginEdit() from there if appropriate.
Apr 14, 2009 at 8:36 PM
The solution you've come up with should work for most scenarios.  However, please consider that your app will almost always close now with a cell in edit mode.  This may or may not cause problems in your implementation, so it's just something to be aware of and to account for.

An alternate solution would be to listen for PreviewKeyDown on DataGrid, and if the key is Enter, then implement the default Enter behavior (commit the cell, move to the next cell, etc - you can look at our source to see what we do), and then additionally put the next cell in edit mode as you described.
Apr 15, 2009 at 7:21 AM
That the window closes with a cell in edit mode is not really a problem in my case, since the business object (a collection of which the grid is bound to) is fairly smart about whether changes have actually been made; if a value is set to the same value as it has from before, that is not considered to be a change that needs to be committed to persistent storage, and other than IEditableObject support, the business objects have no knowledge about batched property edits. This particular business object type doesn't even implement IEditableObject at present, and there might not even be much need for it. Also, this is for a single column (out of four in this particular data grid) and there is a visual cue to the user that something "unusual" is going on in that they can see the combobox in the cell.

All the other options that I can see suffers from the problem of needing to reimplement large parts of the data grid navigation, which has the obvious problem of an oversight (now or later) leading to inconsistent behavior. Then it's better to handle this special case separately (I don't use ComboBoxColumns much; actually this is the only place so far in the application I'm working on, but I can see a few three more places where it might be useful, which is easily accounted for with a helper method call), and account for the fact that a row might be in edit mode when the grid unloads. With how things are implemented on the control consumer side, as long as the user doesn't actually make a change to the data, I don't really see it as a major problem.

In either case, I appreciate the heads-up about the potential issue, and will keep it in mind.