Prevent re-sorting datagrid after editing cell

2020-07-23 04:22发布

问题:

I have a datagrid with editable cells. Im using automatic sorting (by column header). Now when a user edits a cell where the corresponding column has a sorting applied, the datagrid rows will be reordered after the edit is finished, which can be very confusing.

I want to prevent this behaviour, so that the datagrid will be sorted ONLY when clicking on the column header.

Here is my DataGrid XAML:

<DataGrid Name="MyDataGridTable"
            IsReadOnly="False"
            ItemsSource="{Binding Path=MyCollectionView}" 
            AutoGenerateColumns="False"
            SelectionMode="Extended"
            SelectionUnit="Cell">

    <DataGrid.Columns>
        <DataGridTextColumn Header="ID" Binding="{Binding Path=Person.ID}" IsReadOnly="True"></DataGridTextColumn>
        <DataGridTextColumn Header="Age" Binding="{Binding Path=Person.Age}"></DataGridTextColumn>     
        <DataGridTextColumn Header="Name" Binding="{Binding Path=Person.Name}"></DataGridTextColumn>
    </DataGrid.Columns>

</DataGrid>

In my ViewModel I have bound to an ICollectionView:

public ICollectionView MyCollectionView { get; set; }
MyCollectionView = CollectionViewSource.GetDefaultView(_personItemViewModels);    

I already tried listening to events like Sorting, RowEditEnding or CellEditEnding to interrupt the Refresh / Resorting / Refiltering whatever... but couldn't find any solution.