Search code examples
c#wpfdata-bindingsyncfusion

Syncfusion SfDataGrid Cancel SelectedItem


im currently working on a Wpf Application and have a Problem. I would like to Cancel the SelectedItem change and Select the previous Selected item.

So i have a SfDataGrid in my View which has 2 Bindings

  • ItemsSource -> ObservableCollection
  • SelectedItem -> Dummy
<c:ExtendedSfDataGrid x:Name="DummyDataGrid"
                                          BorderThickness="0"
                                          HeaderRowHeight="25"
                                          SelectionMode="Single"
                                          SelectedItem="{Binding SelectedDummy}"
                                          ItemsSource="{Binding DummyCollection}">

And in my ViewModel I have the ObservableCollection and the Dummy object

public ObservableCollection<Dummy> DummyCollection{ get; private set; }
public Dummy SelectedDummy
{
    get=>_selectedDummy;
    set
    {
       if(hasChanges)
       {
          return;
       }
       Set(ref _selectedDummy, value);
    }
}

So how can i cancel the Change and Reselect the previous Dummy ?

I have allready tried to throw a event and change the DummyDataGrid.SelectedItem to the previous one. But in the view the other one is still selected


Solution

  • SfDataGrid.SelectionChanging event occurs before processing the selection to particular row or cell. We can cancel the selection process within this event by setting GridSelectionChangingEventArgs.Cancel property as true.

    UG reference : https://help.syncfusion.com/wpf/datagrid/selection#selectionchanging-event

    Sample link : https://www.syncfusion.com/downloads/support/directtrac/general/ze/SfDataGrid_SelectionChanging645336355