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)); } 
  • You will need a wrapper on your Auto , which will turn off the Auto and IsSelected properties. IsSelected from this wrapper will be bound on ListBox.IsSelected . Or you can try to transfer the selected items as a CommandParameter ... - MihailPw
  • @ AGS17 no implementation of such a wrapper comes to mind. any ideas? - Alexandr
  • In the sense of? Well, I described to you ... AutoViewModel { Auto Item, bool IsSelected } - MihailPw
  • Here’s how stackoverflow.com/a/34632944/5598926 - MihailPw
  • @ AGS17 I apparently did not understand something in the example. even if I get a collection of selected ones, how can I delete them from the database? - Alexandr

1 answer 1

I work this way, the markup:

 <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <ListBox Name="ListBox" ItemsSource="{Binding Autos}" DisplayMemberPath="Model" SelectionMode="Extended" Margin="5,5,5,0"/> <Button Grid.Row="1" Content="Delete" Margin="5" Command="{Binding DeleteCommand}" CommandParameter="{Binding SelectedItems, ElementName=ListBox}"/> </Grid> 

In the VM code:

 public ICommand DeleteCommand => new RelayCommand(o => Delete((Collection<object>)o)); private void Delete(Collection<object> o) { List<Auto> list = o.Cast<Auto>().ToList(); list.ForEach(auto => Autos.Remove(auto)); } 

enter image description here

  • made as at you, object o = null, and only throws an exception. + SelectedItems how many did not search - did not find, only SelectedItem - Alexandr
  • @AlexandrAlexandr, SelectedItems - where you searched for it, if you don’t find it in the ListBox markup, because this property is read-only and it is forbidden for it to assign something in the markup. But you can read from it, which we do by passing it in the command parameter. - Andrey NOP
  • And null cannot be there, even if nothing is selected - there will just be an empty collection. - Andrey NOP
  • I noticed. but the maximum I have achieved is to get 1 element. If you just use your code, it does not transmit anything. why - it is not clear. exactly what o = null. I do not know how it turns out - Alexandr
  • @AlexandrAlexandr, Show me how you used my snippet. - Andrey NOP