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.