There is a ListBox with a list of cars
<ListBox x:Name="autoList" Grid.Column="0" ItemsSource="{Binding Auto}" SelectedItem="{Binding SelectedAuto}"> There is a collection in which data from the ObservableCollection<Auto> Auto; database is located ObservableCollection<Auto> Auto;
how to remove several selected cars in Listbox? To remove 1 car I use the following code
public RelayCommand DeleteCommand { get { return deleteCommand ?? (deleteCommand = new RelayCommand((selectedItem) => { MessageBoxResult result = MessageBox.Show("Вы действительно желаете удалить элемент?", "Удаление", MessageBoxButton.YesNo, MessageBoxImage.Question); if (selectedAuto == null || result == MessageBoxResult.No) return; // получаем выделенный объект Auto auto = selectedAuto as Auto; db.Autos.Remove(auto); db.SaveChanges(); OnPropertyChanged("HasAuto"); }, CanEditOrDeleteAuto)); } } Auto class looks like this (DB table also looks like)
class Auto : INotifyPropertyChanged { private string model; private string marka; private int cost; private int maxSpeed; public int Id { get; set; } public string Model { get { return model; } set { model = value; OnPropertyChanged("Model"); } } public string Marka { get { return marka; } set { marka = value; OnPropertyChanged("Marka"); } } public int Cost { get { return cost; } set { cost = value; OnPropertyChanged("Cost"); } } public int MaxSpeed { get { return maxSpeed; } set { maxSpeed = value; OnPropertyChanged("MaxSpeed"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged([CallerMemberName]string prop = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } DB get this way
public class ApplicationContext : DbContext { public ApplicationContext() : base("DefaultConnection") { } public DbSet<Auto> Autos { get; set; } } Viewmodel
ObservableCollection<Auto> autos; private Auto selectedAuto; public ObservableCollection<Auto> Autos { get { return autos; } set { autos = value; OnPropertyChanged("Autos"); } } public Auto SelectedAuto { get { return selectedAuto; } set { selectedAuto = value; OnPropertyChanged("SelectedAuto"); } } public ApplicationViewModel() { db = new ApplicationContext(); db.Autos.Load(); Autos = db.Autos.Local; } UDP2 markup
<Button Content="Удалить" Margin="10" Command="{Binding DeleteCommand}" Style="{StaticResource InformButton}" CommandParameter="{Binding SelectedItems, ElementName=autoList}" /> VM code
public ICommand DeleteCommand => new RelayCommand(o => Delete((Collection<object>)o)); private void Delete(Collection<object> o) { List<Auto> list = o.Select(e => (Auto)e).ToList(); list.ForEach(auto => Autos.Remove(auto)); } 
Auto, which will turn off theAutoandIsSelectedproperties.IsSelectedfrom this wrapper will be bound onListBox.IsSelected. Or you can try to transfer the selected items as aCommandParameter... - MihailPwAutoViewModel { Auto Item, bool IsSelected }- MihailPw