The ListView is populated with sorted data from the CollectionViewSource, which in turn is taken from the ObservableCollection. Like that:

<CollectionViewSource x:Key="cvsSortByName" Source="{Binding CountryData}"> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName="Name"/> </CollectionViewSource.SortDescriptions> </CollectionViewSource> <ListView ItemsSource="{Binding Source={StaticResource cvsSortByName}} SelectedItem="{Binding SelectedStuff}"/> 

How to make so that after deleting an item the next item in a sorted list is selected? That is, if there is

  index id name 0 1 1 1 3 3 2 4 4 

Add 2

  index id name 0 1 1 3 5 2 - выбранная строка 1 3 3 2 4 4 

Remove 3 5 2 from ObservableCollection.

 CountryData.Remove(item); 

How to make line 1 3 3 selected?

Added SelectedItem, but this does not solve the problem.

Removal piece:

  foreach (CountryModel item in CountryData.Where(s => s.Selected)) { countriesToDelete.Add(new Tuple<int, CountryModel(countryData.IndexOf(item), item)); } for (int i = 0; i < countriesToDelete.Count; i++) { Country countryToDelete = DatabaseModel.SelectByPK<Country>(countriesToDelete[i].Item2.DbId.ToString()); DatabaseModel.Delete(countryToDelete); CountryData.Remove(countriesToDelete[i].Item2); } 

Now, if someone suggested, like here

  s => s.Selected 

Do something like

  s => s.Selected + 1 (я знаю, что так нельзя) 

Then you could just make the last item in the list to delete SelectedStuff and not delete it.

    1 answer 1

    ObservableCollection in itself does not allow working with the current item. To do this, you need to add a separate property to your ViewModel (or what you have in ListView.DataContext). For example:

    XAML windows

     <Window.Resources> <ResourceDictionary> <CollectionViewSource x:Key="cvsSortByName" Source="{Binding CountryData}"> <CollectionViewSource.SortDescriptions> <componentModel:SortDescription PropertyName="Name"/> </CollectionViewSource.SortDescriptions> </CollectionViewSource> </ResourceDictionary> </Window.Resources> <StackPanel> <ListView ItemsSource="{Binding Source={StaticResource cvsSortByName}}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" /> <Button Click="ButtonBase_OnClick" Height="30" Width="30"/> </StackPanel> 

    Some Code Behind for the minimum required linkage with the ViewModel

      private readonly ViewModel vm; public MainWindow() { this.InitializeComponent(); this.DataContext = this.vm = new ViewModel(); } private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { this.vm.DeleteCurrentItemExecute(); } 

    ViewModel with ObservableCollection and SelectedItem properties

     public class ViewModel : INotifyPropertyChanged { private Model selectedItem; public Model SelectedItem { get { return this.selectedItem; } set { if (Equals(value, this.selectedItem)) return; this.selectedItem = value; this.OnPropertyChanged(); } } public ObservableCollection<Model> CountryData { get; } = new ObservableCollection<Model> { new Model {Index = 0, Id = 1, Name = "1"}, new Model {Index = 3, Id = 5, Name = "2"}, new Model {Index = 1, Id = 3, Name = "3"}, new Model {Index = 2, Id = 4, Name = "4"}, }; public void DeleteCurrentItemExecute() { var indexOfSelectedItem = this.CountryData.IndexOf(this.SelectedItem); this.CountryData.Remove(this.SelectedItem); if (this.CountryData.Count == 0) { this.SelectedItem = null; } else { var newIndexOfSelectedItem = indexOfSelectedItem > this.CountryData.Count - 1 ? this.CountryData.Count - 1 : indexOfSelectedItem; this.SelectedItem = this.CountryData[newIndexOfSelectedItem]; } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } 

    Model for your data

     public class Model { public int Index { get; set; } public int Id { get; set; } public string Name { get; set; } } 
    • Thanks for the detailed answer. But it is not that. - mattveiko
    • Suppose I save 3 in a variable. I check. The new index is 2, that is, the last line in the sorted list, and not the next one with the index 1. Somehow it was unexpected that the comment is sent by Enter. - mattveiko
    • Can you describe the script in more detail? What does “save 3 in a variable” mean, what do you “check” and why “new index - 2”? - panchenko.dm
    • All your answer. indexOfSelectedItem - 3. - mattveiko
    • Number of items in the list after deletion - 3 - mattveiko