Hello!
I use the SfDataGrid control in dialog window with MVVM. If I add new row via AddNewRow, then don't press Enter, close Window and open Window again, then I try add new row again and I have exception "Cannot change ObservableCollection during a CollectionChanged event." As I understand, I need CancelAddNewRow programmatically before closing window ( https://help.syncfusion.com/wpf/datagrid/data-manipulation#add-row-programmatically) , but maybe is there more convenient way to do it in MVVM style?
Hi Aksel Nielsen,
We are currently analyzing your reported scenario. We need time to validate the scenario, we will provide an update on or before March 13, 2024.
Hi Aksel Nielsen,
Could you please mention the details of how you are using the dialog window to show the SfDataGrid control.
As of now, we have only two ways to cancel the AddNewRow, programmatically or simply by pressing the Esc key twice.
Hello again. I created example and recorded video. And do next steps:
For example, I can forget to press Esc , so I want to cancel adding new rowprogrammatically, when I close DialogWIndow.
Hi Aksel Nielsen,
Based on the provided information and the sample you provided, we were able to
reproduce the issue.
It seems that in the command for opening the DialogBox, a new instance of
DialogWindow is initialized each time, and similarly, a new instance of
SfDataGrid is also initialized. However, the data context remains the same
instance.
To prevent the issue from occurring, creating a new instance of the viewModel would resolve it.
Additionally, there is currently no other way to cancel the AddNewRow, either by pressing the Esc key or programmatically.
I've found the solution myself. Maybe it will be useful for somebody.
I added event handler to Unloaded event of Window (or UC, it works too).
Unloaded="DialogWindow_OnUnloaded">
And then I set the DataContext property to null
private void DialogWindow_OnUnloaded(object sender, RoutedEventArgs e)
{
((Window)sender).DataContext = null;
}
Hi Aksel Nielsen,
Thanks for the update. We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help.