To reproduce, set up a grid with 2 Columns, each a TemplateColumn with AddNewRowItemTemplate as a ComboBox and ItemTemplate as TextColumn
Run the grid and add a new row by selecting an item from the first ComboBox (which populates the second ComboBox ItemsSource) and then selecting an item from the second ComboBox.
In the RowAdded event the data is present. After this event, SelectionChanged on the first Column's ComboBox is fired and this clears the ItemsSource on the second Column's ComboBox.
In the grid, the new row has no data for the second Column because the binding was lost when the ItemsSource was cleared. The row data should not be lost even in this scenario because it is cleared after the RowAdded event.
Update: Even assigning a new list that includes the items in the previous items source, the SelectedItem binding is lost and the data is not added to the grid.
Hello,
I am just checking if you got this worked out or you still require any assistance or clarification on the matter.
I am just checking the progress of this issue and was wondering if you managed to achieve your goal or if you need any further assistance on the matter.
I've worked around the issue but the bug is nonetheless still present.
I have attached a project reproducing the issue.
Launch the program in and add a new row from the AddNewRow at the bottom by selecting an item in the first combo box ( which populates the second combo box ), then select an item from the second combo box, and then use the C column to add the row ( either by entering some text followed by enter key or just press enter ).
You'll get 2 popups. The first one is the value of the 2nd column in RowAdded event. The second one is the value of the 2nd column in RowExitedEditMode event. In the 2nd popup, the value has become null because the binding has reset the SelectedItem to null when the data object in the AddNewRow was reinitialized.
In the grid you'll also notice that the 2nd column is null.
If we use EditorTemplate instead of AddNewRowItemTemplate, then even in RowAdded the value is null. I am not sure if it is the same in AddNewRowEditorTemplate or not because I did not try it.
----------------------------------------------------------------------
My workaround is to track the added value in RowAdded event and use DataObjectRequested event to reset the value instead of returning an empty new object so that the binding holds the selected value. This allows the combo box to retain the previous selection, but this may not be acceptable in all use cases.
Hello Michael,
I have modified your sample, so now it works correctly. Basically I used AddNewRowEditorTemplate, instead of AddNewRowItemTemplate, because it is the one that is used when you add items. Also I can say that in the RowAdded event, the e.Row is DataRow and the value is not null, while in the RowExitedEditMode, the e.Row is AddNewRow, which is already empty. I handled the RowExitingEditMode, where the values are still in the AddNewRow.
Hope this helps you.
Thanks.
I think I must have used EditorTemplate instead of AddNewRowEditorTemplate previously. EditorTemplate has the same problem as AddNewRowItemTemplate.
However, I'd like the combo box to be visible at all times, which is why i'm using AddNewRowItemTemplate. Is there a way to do this without having the text editors also always in edit mode?
I have modified the sample further, so now it works as you want. In the AddNewRow the ComboBoxes are visible all the time, no matter if the Row is in edit mode or not. Also I handled the XamGrid’s CellEnteredEditMode event and used Dispatcher to open the ComboBox’s DropDown on the first click, because by default it doesn’t open. Please let me know if this helps you or you need further assistance on this matter.
Looking forward for your reply.